ã¯ããã«
ãªããªã åã®èšäºã¯èå³ãããããŸããããããŠãç§ãçŽæããããã«ããã®èšäºã¯7ã»ã°ã¡ã³ãã€ã³ãžã±ãŒã¿ãçµ±åADCã®äœ¿çšäŸã«ã€ããŠèª¬æããŸãããã€ã¯ãã³ã³ãããŒã©ãŒATmega16ã
çè«çåŽé¢
ADCã®äŸãããç解ããã«ã¯ããã®èšäºãèªãããšããå§ãããŸã ã ç§ãã¡ã®å Žåããã¹ãŠã®äŸã§ããµã³ããªã³ã°åšæ³¢æ°ã¯10 Hzã«ãªããŸããããã¯100 msã®ãµã³ããªã³ã°åšæã«çžåœããŸãã ãªããªã Atmega16ãã€ã¯ãã³ã³ãããŒã©ãŒã«çµã¿èŸŒãŸãã10ãããADCã䜿çšãããããéååã¬ãã«ã®æ°ã¯1024ã«ãªããŸããããã¯ãããŒãªã³ã°ãããåããžã¿ã«å€ã®0ã1023ã®å€ã®ç¯å²ã«å¯Ÿå¿ããŸãã ãã¹ãŠã®äŸã®åºæºé»å§ã¯ãå€éšé»å§ã䜿çšãã5 Vã«çãããªããŸããå ¥åã®åºæºé»å§ããäžãäºæžãããã³äžã¯ããããã衚瀺ãããŸããã ADCã®å ¥åé»å§ã5 Vã®å Žåã1023ã®ããžã¿ã«å€ã0 VïŒã°ã©ã³ãïŒã®ããžã¿ã«å€ã¯0ã«ãªããŸããããšãã°ã3.5 Vã®é»å§ã§ã¯ãïŒ3.5 / 5ïŒ= 716ã«çããããžã¿ã«å€ãåŸãããŸããäŸã§ã¯ãéç·åœ¢æ§ããŒããã€ã¢ã¹ã®è£æ£ã¯è¡ããŸããã
枩床ã枬å®ããã«ã¯ã TMP36ã¢ããã°ã»ã³ãµãŒã䜿çšããŸãã ããã¯ãåºåé»å§ãææ°ã®æž©åºŠã«æ£æ¯äŸããäœé»å§ã®é«ç²ŸåºŠæž©åºŠã»ã³ãµãŒã§ãã ATmega16ãã€ã¯ãã³ã³ãããŒã©ã«çµã¿èŸŒãŸãã10ãããADCã䜿çšããŠããã®é»å§ãããžã¿ã«åããŸãã
ããã°ã©ãã³ã°ãšãããã°
æåã®äŸã¯ã000ã999ã®ç¯å²ã®æ°åã®3æå7ã»ã°ã¡ã³ãã€ã³ãžã±ãŒã¿ãžã®åºåã§ããã€ã³ãžã±ãŒã¿ã¯éçã§ãããã0ãã«ãã£ãŠå¶åŸ¡ãããŸãã ã€ãŸã åã»ã°ã¡ã³ãã¯åå¥ã®LEDãšããŠå¶åŸ¡ãããŠãããšèšããŸãã 3ã€ã®7ã»ã°ã¡ã³ãã€ã³ãžã±ãŒã¿ã¯ãããããåå¥ã®ããŒãïŒããŒãAãBãDïŒã«æ¥ç¶ãããŠããŸãã åã ã®ã»ã°ã¡ã³ãã®ãã³çªå·ãžã®æ¥ç¶ã¯ãããŒãã«é¢ä¿ãªãåãã§ãã
æåã®äŸã®ããã°ã©ã ããã¹ã
/* */ #include <ioavr.h> #include <intrinsics.h> #include <ina90.h> /* */ // #define F_CPU 16000000 // #define UCHAR unsigned char #define UINT unsigned int // //0 #define SEG_0 ~(0x3f) //1 #define SEG_1 ~(0x06) //2 #define SEG_2 ~(0x5b) //3 #define SEG_3 ~(0x4F) //4 #define SEG_4 ~(0x66) //5 #define SEG_5 ~(0x6d) //6 #define SEG_6 ~(0x7d) //7 #define SEG_7 ~(0x07) //8 #define SEG_8 ~(0x7F) //9 #define SEG_9 ~(0x6F) //A #define SEG_A ~(0x77) //b #define SEG_b ~(0x7c) //C #define SEG_C ~(0x39) //d #define SEG_d ~(0x5e) //E #define SEG_E ~(0x79) //F #define SEG_F ~(0x71) // #define SEG_MASK (0x7F) // // // #define SEG_1DEC_PORT PORTD // #define SEG_2DEC_PORT PORTB // #define SEG_3DEC_PORT PORTA // #define SEG_1DEC_DDR DDRD // #define SEG_2DEC_DDR DDRB // #define SEG_3DEC_DDR DDRA /* */ // #define DELAY_US(us) __delay_cycles((F_CPU / 1000000) * (us)); // #define DELAY_MS(ms) __delay_cycles((F_CPU / 1000) * (ms)); // #define SEG_PORTS_INIT() ( SEG_3DEC_DDR |= SEG_MASK );\ ( SEG_2DEC_DDR |= SEG_MASK );\ ( SEG_1DEC_DDR |= SEG_MASK ); // #define SEG_PORTS_CLEAR() ( SEG_3DEC_PORT &=~ SEG_MASK );\ ( SEG_2DEC_PORT &=~ SEG_MASK );\ ( SEG_1DEC_PORT &=~ SEG_MASK ); // #define SEG_PORTS_OUT(x,y,z) ( SEG_3DEC_PORT |= ( x & SEG_MASK ) );\ ( SEG_2DEC_PORT |= ( y & SEG_MASK ) );\ ( SEG_1DEC_PORT |= ( z & SEG_MASK ) ); /* */ // const unsigned char numbers[16] = { SEG_0, //0 SEG_1, //1 SEG_2, //2 SEG_3, //3 SEG_4, //4 SEG_5, //5 SEG_6, //6 SEG_7, //7 SEG_8, //8 SEG_9, //9 SEG_A, //A SEG_b, //b SEG_C, //C SEG_d, //d SEG_E, //E SEG_F //F }; // UINT i = 0; /* ** Name: Seg_Write() ** Description: ** ** Parameters: UINT dec3number 0 - 999 ** Returns: none */ void Seg_Write(UINT dec3number) { // UCHAR dec3 = 0 , dec2 = 0 , dec1 = 0; // 4 // dec3number = dec3number % 1000; // dec3 = dec3number / 100; // dec3number = dec3number % 100; // dec2 = dec3number / 10; // dec1 = dec3number % 10; // // SEG_PORTS_CLEAR(); // 3- // // SEG_PORTS_OUT( numbers[dec3], // numbers[dec2], // numbers[dec1] // ); }//end func /* ** Name: main() ** Description: , ** Parameters: none ** Returns: none */ // void main( void ) { // // SEG_PORTS_INIT(); // Seg_Write(888); // DELAY_MS(3000); // i=0; // for(;;) { // Seg_Write(i); // i++; // ? // if (i == 1000) i=0; // 100 DELAY_MS(100); }//end for }
æ°å€ãåºåããã«ã¯ããïŒ0x3fïŒã®åœ¢åŒã®16é²ã³ãŒãã䜿çšãããŸããåã³ãŒãã¯ã0ã9ã®ç¯å²ã®ç¹å®ã®10é²æ°ã«å¯Ÿå¿ããŸãã ãã®ãããªã³ãŒãããšã«ããã©ãŒã SEG_Xã®å®çŸ©ãäœæãããŸããXã¯è¡šç€ºãããæ°åã§ãã ãããã®16é²ã³ãŒãã¯ãé åã€ã³ããã¯ã¹ã®å€ã衚瀺ãããæ°åã«å¯Ÿå¿ããããã«ãæ°å€[16]é åã«åéãããŸãã ããšãã°ãæ°å€[3]é åã®3çªç®ã®èŠçŽ ãããŒãã«æžã蟌ããšãæ°å€3ã€ã³ãžã±ãŒã¿ãŒã®åºåãæäŸãããŸãã
ãã¯ãé¢æ°SEG_PORTS_INITïŒïŒã䜿çšãããšãããŒãAãBãDã®äœ¿çšæžã¿åºåãåºåçšã«æ§æã§ããŸãã 8çªç®ä»¥å€ã®ãã¹ãŠã®ããŒããã³ã䜿çšãããŸãã ãã¯ãé¢æ°SEG_PORTS_CLEARïŒïŒã¯ã7ã»ã°ã¡ã³ãã€ã³ãžã±ãŒã¿ãŒã®ããŒããã¯ãªã¢ãããã¹ãŠã®åºåãã0ãã«ãªã»ããããŸãã ãã®ãã¯ãé¢æ°ã¯ãSEG_PORTS_OUTïŒxãyãzïŒãšãšãã«ããã¹ã¯ãšéžèšãçµã¿åãããŠ3ã€ãã¹ãŠã®ããŒãã«å€ãæžã蟌ãããšã§ãå€ãé«ãããã«æå·ãäžããããšãªãå®å šã«ããŒãã«æžã蟌ãŸããããã«ããŸãã
䟿å®äžã3æ¡ã®æ°åã®åºåã«ã¯void Seg_WriteïŒUINT dec3numberïŒé¢æ°ã䜿çšãããŸããããã§ãdec3numberã¯è¡šç€ºããã3æ¡ã®æ°åã§ãã é¢æ°ã§ã¯ãæŽæ°/ã«ããé€ç®ã®æäœãšãé€ç®ã®å°äœïŒ ãååŸããæäœã䜿çšããŠã30幎ããšã®æ°å€ã®ãããããéžæãããŸãã ãã®åŸãå10幎ã®å€ã¯æ°å[decX]é åã®ã€ã³ããã¯ã¹ãšããŠäœ¿çšãããŸããããã§ãdecXã¯3幎ã®1ã€ã§ãããSEG_PORTS_OUTïŒxãyãzïŒã䜿çšããŠæ°åã®äœçœ®ã«åŸã£ãŠããŒãã«æžã蟌ãŸããŸãã
ããã°ã©ã ã®ã¡ã€ã³ã«ãŒãã§ã¯ãå€æ°iã®å€ãé 次åºåãããå€999ãè¶ ãããšå¢åãããŠãŒãã«ãªããŸãifã¹ããŒãã¡ã³ãã¯ããã®ç¶æ ãåæããããã«äœ¿çšãããŸãã å€æ°iã®å€ã®åºåã¯ã100ããªç§ã®ééã§çºçããŸããããã¯ãåã®èšäºã§æ¢ã«èª¬æããé 延ãã¯ãé¢æ°DELAY_MSïŒ100ïŒã«ãã£ãŠæäŸãããŸãã
ãããã°äžã«ãå€æ°iã®ã·ãŒã±ã³ã·ã£ã«ãªå¢åãããã³ãã®å€æ°ã®å€ã16é²ã³ãŒããSeg_WriteïŒïŒé¢æ°ã䜿çšããŠå€æãããããšã確èªã§ããŸãã
2çªç®ã®äŸã¯ãçµ±åADCã䜿çšããŠããŸãã ã¢ããã°ä¿¡å·ã¯7çªç®ã®ãã£ãã«ã«æ¥ç¶ããããã®å ¥åã¯ããŒãAã®7çªç®ã®ãã³ã«ããããæ¥ç¶ãããŸãã
2çªç®ã®äŸã®ããã°ã©ã ããã¹ã
/* */ #include <ioavr.h> #include <intrinsics.h> #include <ina90.h> /* */ // #define F_CPU 16000000 // #define UINT unsigned int /* */ // #define DELAY_US(us) __delay_cycles((F_CPU / 1000000) * (us)); // #define DELAY_MS(ms) __delay_cycles((F_CPU / 1000) * (ms)); /* */ // - UINT ADC_Result = 0; /* */ // void main( void ) { // // // 7- D DDRD_DDD7 = 1; // 7- D "0" PORTD_PORTD7 = 0; // // 7- A DDRA_DDA7 = 0; // PORTA_PORTA7 = 0; ADMUX_MUX0 = 1; // ADMUX_MUX1 = 1; ADMUX_MUX2 = 1; ADCSRA_ADEN = 1; // ADCSRA_ADPS0 = 1; // 16 / 128 = 125 ADCSRA_ADPS1 = 1; // 50-200 ADCSRA_ADPS2 = 1; // 13 125 13 / 125000 = 104 // for(;;) { // _NOP(); // 7- D "0" "1" "1" "0" // PORTD_PORTD7 ^= 1; // - ADCSRA_ADSC = 1; // - ? // (104 ) while (ADCSRA_ADIF == 0); // _NOP(); // - ADC_Result = ADC; // //ADC_Result = ADCL; // //ADC_Result += (ADCH << 8); // // ADCSRA_ADIF=1; // 100 (1/0,1 = 10 - ) DELAY_MS(100); }//end for }//end main
ãã®äŸã§ã¯ãã¡ã€ã³ïŒïŒé¢æ°ã§ãåã®èšäºã§æ¢ã«èª¬æããLEDãèšå®ããåŸãããŒãDã®7çªç®ã®åºåãå ¥åçšã«æ§æãããŸãã ãã®ãããDDRDå¶åŸ¡ã¬ãžã¹ã¿ã®DDD7ãããã¯ã0ãã«ãªã»ãããããŸãã ãã®åŸãPORTAããŒã¿ã¬ãžã¹ã¿ã®ãPORTA7ãããããã0ãã«ãªã»ããããããšã«ããããã®ãã³ã®ãã«ã¢ããæµæã匷å¶çã«åæãããŸãã
次ã«ãADMUXå¶åŸ¡ã¬ãžã¹ã¿ã®MUXxãããã«ã1ããæžã蟌ãããšã«ããã7çªç®ã®ADCãã£ãã«ãéžæãããŸãã
次ã«ãADCSRAå¶åŸ¡ã¬ãžã¹ã¿ã®ADENããããèšå®ããŠãADCããªã³ã«ããŸãã
ADCSRAå¶åŸ¡ã¬ãžã¹ã¿ããŒãã®ADPSxãããã¯ã1ãã«èšå®ãããADCã50ã200 kHzã®æé©ãªåšæ³¢æ°ç¯å²ã§åäœããããšãä¿èšŒããŸãïŒããŒã¿ã·ãŒãã«èšèŒïŒã ADPSxãããã¯ã128ã®ããªã¹ã±ãŒã©ãŒå€ãæäŸããŸãã16MHzã®ã¯ããã¯åšæ³¢æ°ã§ã®ç¹å®ã®åšæ³¢æ°å€ã¯ã16 MHz / 128 = 128 kHzã§ãã
å¿ èŠãªèšå®ããã¹ãŠå®äºãããšããã©ã°ã䜿çšããADå€æã®åšæçãªé 次éå§ãšããŒãªã³ã°ãè¡ãããŸãã LEDã¯ãããã°ã©ã æäœã€ãã³ãã瀺ãããã«äœ¿çšãããŸãã
ADå€æã¯ãADSRAå¶åŸ¡ã¬ãžã¹ã¿ã®ADSCããããã»ããããããšã«ããéå§ãããŸãã çµæã®æºåã¯ããã®å¶åŸ¡ã¬ãžã¹ã¿ã®ADIFãã©ã°ãèšå®ããããšã«ãã決å®ãããŸãã ADCã®éžæãããåšæ³¢æ°ã§ã1åã®å€æã¯104ÎŒsç¶ããŸãã ADIFãã©ã°ãèšå®ããåŸãADå€æã®çµæïŒç¯å²ã¯0ã1023ïŒã¯ãADCLããã³ADCHããŒã¿ã¬ãžã¹ã¿ããèªã¿åãããšãã§ããŸãã ADå€æã®çµæã¯ãADC_Resultå€æ°ã«æžã蟌ãŸããŸãã 次ã«ããšã³ããªã1ãã§ADIFãã©ã°ãã¯ãªã¢ããå¿ èŠããããŸãã ããŒãªã³ã°ééã¯100ããªç§ã§ã10 Hzã®ãµã³ããªã³ã°ã¬ãŒãã«çžåœããŸãã
ãããã°äžã«ãADCèšå®ã®æ£ç¢ºæ§ããã¹ãããããADå€æã®çµæãšããŠåŸãããéåã®å€ãè©äŸ¡ãããã§ããŸãã
3çªç®ã®äŸãADC調æ»ã§ãããè¿œå ã®ãã¯ãé¢æ°ã䜿çšããŠããŸãã
3çªç®ã®äŸã®ããã°ã©ã ããã¹ã
/* */ #include <ioavr.h> #include <intrinsics.h> #include <ina90.h> /* */ // #define F_CPU 16000000 // #define UINT unsigned int // // #define LED_DDR DDRD #define LED_PORT PORTD #define LED_PIN DDD7 // #define ADC_IN_DDR DDRA #define ADC_IN_PORT PORTA #define ADC_IN_DDR_PIN DDA7 #define ADC_IN_PORT_PIN PORTA7 // #define ADC0 (0<<MUX2)|(0<<MUX1)|(0<<MUX0) #define ADC1 (0<<MUX2)|(0<<MUX1)|(1<<MUX0) #define ADC2 (0<<MUX2)|(1<<MUX1)|(0<<MUX0) #define ADC3 (0<<MUX2)|(1<<MUX1)|(1<<MUX0) #define ADC4 (1<<MUX2)|(0<<MUX1)|(0<<MUX0) #define ADC5 (1<<MUX2)|(0<<MUX1)|(1<<MUX0) #define ADC6 (1<<MUX2)|(1<<MUX1)|(0<<MUX0) #define ADC7 (1<<MUX2)|(1<<MUX1)|(1<<MUX0) // #define ADC_F_CPU_DIV_2 (0<<ADPS2)|(0<<ADPS1)|(1<<ADPS0) #define ADC_F_CPU_DIV_4 (0<<ADPS2)|(0<<ADPS1)|(1<<ADPS0) #define ADC_F_CPU_DIV_8 (0<<ADPS2)|(1<<ADPS1)|(0<<ADPS0) #define ADC_F_CPU_DIV_16 (0<<ADPS2)|(1<<ADPS1)|(1<<ADPS0) #define ADC_F_CPU_DIV_32 (1<<ADPS2)|(0<<ADPS1)|(0<<ADPS0) #define ADC_F_CPU_DIV_64 (1<<ADPS2)|(1<<ADPS1)|(0<<ADPS0) #define ADC_F_CPU_DIV_128 (1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0) /* */ // #define LED_INIT() ( LED_DDR |= (1<<LED_PIN) ); // #define LED_LOW() ( LED_PORT &=~ (1<<LED_PIN) ); // #define LED_HIGH() ( LED_PORT |= (1<<LED_PIN) ); // #define LED_TOG() ( LED_PORT ^= (1<<LED_PIN) ); // #define ADC_IN_INIT() ( ADC_IN_DDR &= ~(0<<ADC_IN_DDR_PIN) );\ ( ADC_IN_PORT |= (0<<ADC_IN_PORT_PIN) ); // #define ADC_SET_CHAN(x) ( ADMUX |= x ); // #define ADC_ON() ( ADCSRA |= (1<<ADEN) ); // #define ADC_OFF() ( ADCSRA &=~ (1<<ADEN) ); // #define ADC_SET_CLK_DIV(x) ( ADCSRA |= x ); // - #define ADC_START_CONV() ( ADCSRA |= (1<<ADSC) ); // - #define ADC_RES_READY() ( ADCSRA & (1<<ADIF) ) // - #define ADC_FLAG_CLEAR() ( ADCSRA |= (1<<ADIF) ); // #define DELAY_US(us) __delay_cycles((F_CPU / 1000000) * (us)); // #define DELAY_MS(ms) __delay_cycles((F_CPU / 1000) * (ms)); /* */ // - UINT ADC_Result = 0; /* */ // void main( void ) { // // LED_INIT(); // LED_LOW(); // // ADC_IN_INIT(); // ADC_SET_CHAN(ADC7); ADC_ON(); // // 16 / 128 = 125 ADC_SET_CLK_DIV(ADC_F_CPU_DIV_128); // 50-200 // 13 125 13 / 125000 = 104 // for(;;) { // _NOP(); // LED_TOG(); // - ADC_START_CONV(); // - ? // (104 ) while(!ADC_RES_READY()); // _NOP(); // - ADC_Result = ADC; // //ADC_Result = ADCL; // //ADC_Result += (ADCH << 8); // // ADC_FLAG_CLEAR(); // 100 (1/0,1 = 10 - ) DELAY_MS(100); }//end for }//end main
ADCã®7çªç®ã®ãã£ã³ãã«ã®å ¥åã¯ããã¯ãé¢æ°ADC_IN_INITïŒïŒã䜿çšããŠåæåãããŸãã ADC_IN_DDRãADC_IN_PORTãADC_IN_DDR_PINãADC_IN_PORT_PINã®å®çŸ©ã¯ãåºåãšãããã«å¿ããŠADCãã£ã³ãã«ãç°¡åã«å€æŽã§ããããã«ããŸãã
ãããã¯ãADMUXãã£ãã«éžæå¶åŸ¡ã¬ãžã¹ã¿ã§èšå®ããããã¯ãé¢æ°ADC_SET_CHANïŒADCxïŒã䜿çšããŠæ§æãããŸããADCxã®åŒæ°ã¯ãã£ãã«çªå·ã決å®ããŸããxã¯0ã7ã®ç¯å²ã§éžæãããŸãã åADCxå®çŸ©ã¯ãå€ã0ãããã³ã1ããéžæããããã£ãã«çªå·xã«å¯Ÿå¿ããMUXxå¶åŸ¡ãããã®éåã§ãã
ADCã¯ãADC_ONïŒïŒãã¯ãé¢æ°ã«ãã£ãŠãªã³ã«ãªããŸãã ååšåšã¯ããã¯ãé¢æ°ADC_SET_CLK_DIVïŒADC_F_CPU_DIV_xïŒã䜿çšããŠéžæãããŸããåŒæ°ã¯ãADC_F_CPU_DIV_xã®å®çŸ©ã®æ°ããéžæãããŸãã
ADå€æã¯ããã¯ãé¢æ°ADC_START_CONVïŒïŒã«ãã£ãŠèµ·åãããŸãã ADå€æã®æºåã¯ããã¯ãé¢æ°ADC_RES_READYïŒïŒã«ãã£ãŠæ±ºå®ãããŸãã ADC_FLAG_CLEARïŒïŒã§ãã©ã°ãã¯ãªã¢ããŸãã
4çªç®ã®äŸã¯ãå²ã蟌ã¿ã¡ã«ããºã ã䜿çšããŠãã©ã°ã§ã¯ãªãæºåå®äºã€ãã³ãããã£ããã£ããADCããŒãªã³ã°ã§ãã
4çªç®ã®äŸã®ããã°ã©ã ããã¹ã
/* */ #include <ioavr.h> #include <intrinsics.h> #include <ina90.h> /* */ // #define F_CPU 16000000 // #define UINT unsigned int /* */ // #define DELAY_US(us) __delay_cycles((F_CPU / 1000000) * (us)); // #define DELAY_MS(ms) __delay_cycles((F_CPU / 1000) * (ms)); /* */ // - UINT ADC_Result = 0; /* */ // void main( void ) { // // // 7- D DDRD_DDD7 = 1; // 7- D "0" PORTD_PORTD7 = 0; // // 7- A DDRA_DDA7 = 0; // PORTA_PORTA7 = 1; ADMUX_MUX0 = 1; // ADMUX_MUX1 = 1; ADMUX_MUX2 = 1; ADCSRA_ADEN = 1; // ADCSRA_ADPS0 = 1; // 16 / 128 = 125 ADCSRA_ADPS1 = 1; // 50-200 ADCSRA_ADPS2 = 1; // 13 125 13 / 125000 = 104 ADCSRA_ADIE = 1; // // _SEI(); // for(;;) { // _NOP(); // 7- D "0" "1" "1" "0" // PORTD_PORTD7 ^= 1; // - ADCSRA_ADSC = 1; // 100 (1/0,1 = 10 - ) DELAY_MS(100); }//end for }//end main /* -*/ #pragma vector=ADC_vect __interrupt void ISR_ADC(void) { // _NOP(); // - ADC_Result = ADC; // //ADC_Result = ADCL; // //ADC_Result += (ADCH << 8); // }//end func
ADå€æã®çµæã®æºåãã§ãããšãã«å²ã蟌ã¿ãæå¹ã«ããã«ã¯ãADCSRAå¶åŸ¡ã¬ãžã¹ã¿ã®ADIEããããèšå®ããå¿ èŠããããŸãã ãã®åŸããã¯ãé¢æ°_SEIïŒïŒã䜿çšããŠããã¹ãŠã®ãã¹ã¯ãããå²ã蟌ã¿ã®ã°ããŒãã«ãªè§£æ±ºãè¡ããŸãã
ããã°ã©ã ã®ããžãã¯ã¯ãã¡ã€ã³ã«ãŒããšå²ã蟌ã¿ãã³ãã©ãŒã®æºäžŠååäœãæäŸããã¹ãŒããŒãµã€ã¯ã«ãåããã·ã¹ãã ã§ããæåã®èšäºã§æ¢ã«èª¬æãããŠããŸãã ã¡ã€ã³ã«ãŒãã§ã¯ãADå€æã¯100ããªç§ã®åšæã§éå§ããŸãã çŽ104ÎŒsã®ééã®åŸãã¡ã€ã³ãµã€ã¯ã«ã¯ãADå€æçµæã®æºåã€ãã³ãã«ãã£ãŠå²ã蟌ã¿åŠçã®ããã«äžæãããŸãã å²ã蟌ã¿åŠçã¯ããã®ã€ãã³ãã§åŒã³åºãããISR_ADCïŒïŒãã³ãã©ãŒé¢æ°ã䜿çšããŠå®è¡ãããŸãã ADå€æã®çµæã¯ããã³ãã©ãŒé¢æ°ã®æ¬äœã®ADCLããã³ADCHããŒã¿ã¬ãžã¹ã¿ããã³ããŒãããŸãã
ãããã°äžã«ãADCåšèŸºã¢ãžã¥ãŒã«ã®å²ã蟌ã¿ãµãã·ã¹ãã ã®æ£ããåäœããã¹ãã§ããŸãã
5çªç®ã®äŸã¯ã3çªç®ãš4çªç®ã®äŸã®å ±çã§ãã ããã¯ããã¯ãé¢æ°ã䜿çšããADCåãåããã§ãã
5çªç®ã®äŸã®ããã°ã©ã ããã¹ã
/* */ #include <ioavr.h> #include <intrinsics.h> #include <ina90.h> /* */ // #define F_CPU 16000000 // #define UINT unsigned int // // #define LED_DDR DDRD #define LED_PORT PORTD #define LED_PIN DDD7 // #define ADC_IN_DDR DDRA #define ADC_IN_PORT PORTA #define ADC_IN_DDR_PIN DDA7 #define ADC_IN_PORT_PIN PORTA7 // #define ADC0 (0<<MUX2)|(0<<MUX1)|(0<<MUX0) #define ADC1 (0<<MUX2)|(0<<MUX1)|(1<<MUX0) #define ADC2 (0<<MUX2)|(1<<MUX1)|(0<<MUX0) #define ADC3 (0<<MUX2)|(1<<MUX1)|(1<<MUX0) #define ADC4 (1<<MUX2)|(0<<MUX1)|(0<<MUX0) #define ADC5 (1<<MUX2)|(0<<MUX1)|(1<<MUX0) #define ADC6 (1<<MUX2)|(1<<MUX1)|(0<<MUX0) #define ADC7 (1<<MUX2)|(1<<MUX1)|(1<<MUX0) // #define ADC_F_CPU_DIV_2 (0<<ADPS2)|(0<<ADPS1)|(1<<ADPS0) #define ADC_F_CPU_DIV_4 (0<<ADPS2)|(0<<ADPS1)|(1<<ADPS0) #define ADC_F_CPU_DIV_8 (0<<ADPS2)|(1<<ADPS1)|(0<<ADPS0) #define ADC_F_CPU_DIV_16 (0<<ADPS2)|(1<<ADPS1)|(1<<ADPS0) #define ADC_F_CPU_DIV_32 (1<<ADPS2)|(0<<ADPS1)|(0<<ADPS0) #define ADC_F_CPU_DIV_64 (1<<ADPS2)|(1<<ADPS1)|(0<<ADPS0) #define ADC_F_CPU_DIV_128 (1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0) /* */ // #define LED_INIT() ( LED_DDR |= (1<<LED_PIN) ); // #define LED_LOW() ( LED_PORT &=~ (1<<LED_PIN) ); // #define LED_HIGH() ( LED_PORT |= (1<<LED_PIN) ); // #define LED_TOG() ( LED_PORT ^= (1<<LED_PIN) ); // #define ADC_IN_INIT() ( ADC_IN_DDR &= ~(0<<ADC_IN_DDR_PIN) );\ ( ADC_IN_PORT |= (0<<ADC_IN_PORT_PIN) ); // #define ADC_SET_CHAN(x) ( ADMUX |= x ); // #define ADC_ON() ( ADCSRA |= (1<<ADEN) ); // #define ADC_OFF() ( ADCSRA &=~ (1<<ADEN) ); // #define ADC_SET_CLK_DIV(x) ( ADCSRA |= x ); // - #define ADC_START_CONV() ( ADCSRA |= (1<<ADSC) ); // #define ADC_INT_ON() ( ADCSRA |= (1<<ADIE) ); // #define DELAY_US(us) __delay_cycles((F_CPU / 1000000) * (us)); // #define DELAY_MS(ms) __delay_cycles((F_CPU / 1000) * (ms)); /* */ // - UINT ADC_Result = 0; /* */ // void main( void ) { // // LED_INIT(); // LED_LOW(); // // ADC_IN_INIT(); // ADC_SET_CHAN(ADC7); ADC_ON(); // // 16 / 128 = 125 ADC_SET_CLK_DIV(ADC_F_CPU_DIV_128); // 50-200 // 13 125 13 / 125000 = 104 ADC_INT_ON(); // // _SEI(); // for(;;) { // _NOP(); // LED_TOG(); // - ADC_START_CONV(); // 100 (1/0,1 = 10 - ) DELAY_MS(100); }//end for }//end main /* -*/ #pragma vector=ADC_vect __interrupt void ISR_ADC(void) { // _NOP(); // - ADC_Result = ADC; // //ADC_Result = ADCL; // //ADC_Result += (ADCH << 8); // }//end func
ãã¯ãé¢æ°ADC_INT_ONïŒïŒã¯ãå²ã蟌ã¿ãæå¹ã«ããããã«äœ¿çšãããŸãã
å¿ èŠãªãã¹ãŠã®äŸãæ€èšããã®ã§ããã€ã¯ãã³ã³ãããŒã©ãŒã®å éšåšèŸºãŠããããæäœããæ¢ã«æ€èšãããäŸã«åºã¥ããŠäœæãããããžã¿ã«æž©åºŠèšã®ãããžã§ã¯ãå šäœã«é²ã¿ãŸãã
ããžã¿ã«æž©åºŠèšããã°ã©ã ããã¹ã
/* */ #include <ioavr.h> #include <intrinsics.h> #include <ina90.h> #include <stdbool.h> /* */ // #define F_CPU 16000000 // #define UCHAR unsigned char #define UINT unsigned int #define FLOAT_TYPE float // // #define LED_DDR DDRD #define LED_PORT PORTD #define LED_PIN DDD7 // #define F_CPU_DIV_1 (0<<CS02)|(0<<CS01)|(1<<CS00) #define F_CPU_DIV_8 (0<<CS02)|(1<<CS01)|(0<<CS00) #define F_CPU_DIV_64 (0<<CS02)|(1<<CS01)|(1<<CS00) #define F_CPU_DIV_256 (1<<CS02)|(0<<CS01)|(0<<CS00) #define F_CPU_DIV_1024 (1<<CS02)|(0<<CS01)|(1<<CS00) // #define TCNT0_VALUE 99 // #define T0_TICK_CNT_LIMIT 10 // #define ADC_IN_DDR DDRA #define ADC_IN_PORT PORTA #define ADC_IN_DDR_PIN DDA7 #define ADC_IN_PORT_PIN PORTA7 // #define ADC0 (0<<MUX2)|(0<<MUX1)|(0<<MUX0) #define ADC1 (0<<MUX2)|(0<<MUX1)|(1<<MUX0) #define ADC2 (0<<MUX2)|(1<<MUX1)|(0<<MUX0) #define ADC3 (0<<MUX2)|(1<<MUX1)|(1<<MUX0) #define ADC4 (1<<MUX2)|(0<<MUX1)|(0<<MUX0) #define ADC5 (1<<MUX2)|(0<<MUX1)|(1<<MUX0) #define ADC6 (1<<MUX2)|(1<<MUX1)|(0<<MUX0) #define ADC7 (1<<MUX2)|(1<<MUX1)|(1<<MUX0) // #define ADC_F_CPU_DIV_2 (0<<ADPS2)|(0<<ADPS1)|(1<<ADPS0) #define ADC_F_CPU_DIV_4 (0<<ADPS2)|(0<<ADPS1)|(1<<ADPS0) #define ADC_F_CPU_DIV_8 (0<<ADPS2)|(1<<ADPS1)|(0<<ADPS0) #define ADC_F_CPU_DIV_16 (0<<ADPS2)|(1<<ADPS1)|(1<<ADPS0) #define ADC_F_CPU_DIV_32 (1<<ADPS2)|(0<<ADPS1)|(0<<ADPS0) #define ADC_F_CPU_DIV_64 (1<<ADPS2)|(1<<ADPS1)|(0<<ADPS0) #define ADC_F_CPU_DIV_128 (1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0) // //0 #define SEG_0 ~(0x3f) //1 #define SEG_1 ~(0x06) //2 #define SEG_2 ~(0x5b) //3 #define SEG_3 ~(0x4F) //4 #define SEG_4 ~(0x66) //5 #define SEG_5 ~(0x6d) //6 #define SEG_6 ~(0x7d) //7 #define SEG_7 ~(0x07) //8 #define SEG_8 ~(0x7F) //9 #define SEG_9 ~(0x6F) //A #define SEG_A ~(0x77) //b #define SEG_b ~(0x7c) //C #define SEG_C ~(0x39) //d #define SEG_d ~(0x5e) //E #define SEG_E ~(0x79) //F #define SEG_F ~(0x71) // #define SEG_GRAD ~(0x63) // #define SEG_MASK (0x7F) // // // #define SEG_1DEC_PORT PORTD // #define SEG_2DEC_PORT PORTB // #define SEG_3DEC_PORT PORTA // #define SEG_1DEC_DDR DDRD // #define SEG_2DEC_DDR DDRB // #define SEG_3DEC_DDR DDRA /* */ // #define LED_INIT() ( LED_DDR |= (1<<LED_PIN) ); // #define LED_LOW() ( LED_PORT &=~ (1<<LED_PIN) ); // #define LED_HIGH() ( LED_PORT |= (1<<LED_PIN) ); // #define LED_TOG() ( LED_PORT ^= (1<<LED_PIN) ); // #define TIMER0_SET_CLK_DIV(x) ( TCCR0 |= x ); // #define TIMER0_SET_CNT(x) ( TCNT0 = x ); // #define TIMER0_OVF_INT_ON() ( TIMSK|=(1<<TOIE0) ); // #define ADC_IN_INIT() ( ADC_IN_DDR &= ~(0<<ADC_IN_DDR_PIN) );\ ( ADC_IN_PORT &= ~(0<<ADC_IN_PORT_PIN) ); // #define ADC_SET_CHAN(x) ( ADMUX |= x ); // #define ADC_ON() ( ADCSRA |= (1<<ADEN) ); // #define ADC_OFF() ( ADCSRA &=~ (1<<ADEN) ); // #define ADC_SET_CLK_DIV(x) ( ADCSRA |= x ); // - #define ADC_START_CONV() ( ADCSRA |= (1<<ADSC) ); // #define ADC_INT_ON() ( ADCSRA |= (1<<ADIE) ); // #define SEG_PORTS_INIT() ( SEG_3DEC_DDR |= SEG_MASK );\ ( SEG_2DEC_DDR |= SEG_MASK );\ ( SEG_1DEC_DDR |= SEG_MASK ); // #define SEG_PORTS_CLEAR() ( SEG_3DEC_PORT &=~ SEG_MASK );\ ( SEG_2DEC_PORT &=~ SEG_MASK );\ ( SEG_1DEC_PORT &=~ SEG_MASK ); // #define SEG_PORTS_OUT(x,y,z) ( SEG_3DEC_PORT |= ( x & SEG_MASK ) );\ ( SEG_2DEC_PORT |= ( y & SEG_MASK ) );\ ( SEG_1DEC_PORT |= ( z & SEG_MASK ) ); // #define DELAY_US(us) __delay_cycles((F_CPU / 1000000) * (us)); // #define DELAY_MS(ms) __delay_cycles((F_CPU / 1000) * (ms)); /* */ // T0 UINT T0_tick_cnt=0; // const unsigned char numbers[16] = { SEG_0, //0 SEG_1, //1 SEG_2, //2 SEG_3, //3 SEG_4, //4 SEG_5, //5 SEG_6, //6 SEG_7, //7 SEG_8, //8 SEG_9, //9 SEG_A, //A SEG_b, //b SEG_C, //C SEG_d, //d SEG_E, //E SEG_F //F }; // FLOAT_TYPE T = 0; // - UINT ADC_Result = 0; // - - UINT ADC_res_cnt = 0; // UINT Sum = 0; // - bool ADCReadyFlag = false; /* ** Name: Seg_Write() ** Description: ** ** Parameters: UINT T - 0 - 99 C ** Returns: none */ void Seg_Write(UINT T) { // UINT dec2 = 0 , dec1 = 0; // T = T % 100; // dec2 = T / 10; // dec1 = T % 10; // // SEG_PORTS_CLEAR(); // 3- // // SEG_PORTS_OUT( numbers[dec2], // numbers[dec1], // SEG_GRAD // ); }//end func /* ** Name: main() ** Description: , ** Parameters: none ** Returns: none */ // void main( void ) { // // SEG_PORTS_INIT(); // LED_INIT(); // LED_LOW(); // ( Normal) TIMER0_SET_CLK_DIV(F_CPU_DIV_1024);// 16 000 000 // 16 000 000 / 1024 = 15 625 // 1 / 15 625 = 0,000064 =64 TIMER0_SET_CNT(TCNT0_VALUE); // 156 * 0,000064 c = 0,009984 c (10 ) // 255-156 = 99 TIMER0_OVF_INT_ON(); // // // ADC_IN_INIT(); // ADC_SET_CHAN(ADC7); ADC_ON(); // // 16 / 128 = 125 ADC_SET_CLK_DIV(ADC_F_CPU_DIV_128); // 50-200 // 13 125 13 / 125000 = 104 ADC_INT_ON(); // // T=0; // Sum = 0; // _SEI(); // for(;;) { // _NOP(); // - // if(ADCReadyFlag) { // _NOP(); // ADCReadyFlag = false; // LED_TOG(); // //- Sum += ADC_Result; // //- ADC_res_cnt++; // -? if ( ADC_res_cnt == 8 ) { // _NOP(); // ADC_res_cnt = 0; // ADC_Result = Sum / 8; // Sum = 0; // T = ADC_Result*(5.0/1023); // T = ((T-0.75)*100)+25; // // Seg_Write((UINT)T); }//end if }//end if }//end for }//end main /* T0 */ #pragma vector=TIMER0_OVF_vect __interrupt void ISR_TickTimer(void) { // _NOP(); // T0 T0_tick_cnt++; // 100 if (T0_tick_cnt >= T0_TICK_CNT_LIMIT) { // _NOP(); // T0 T0_tick_cnt=0; // // - ADC_START_CONV(); }//end for // // TIMER0_SET_CNT(TCNT0_VALUE); }//end func /* -*/ #pragma vector=ADC_vect __interrupt void ISR_ADC(void) { // _NOP(); // - ADC_Result = ADC; // //ADC_Result = ADCL; // //ADC_Result += (ADCH << 8); // // - ADCReadyFlag = true; }//end func
åšèŸºã¬ãžã¹ã¿ã®èšå®ã«é¢ããŠã¯ããã¯ãæ©èœã§ãã§ã«èª¬æããLEDãããŒããŠã§ã¢ã¿ã€ããŒãADCã®èšå®ã䜿çšãããŸãã
ããã°ã©ã ã®ããžãã¯ã¯ãã¡ã€ã³ã«ãŒãã®ç䌌䞊ååäœãšããã£ãã¯ããŒããŠã§ã¢ã¿ã€ããŒããã³ADCçšã®2ã€ã®å²ã蟌ã¿ãã³ãã©ãŒã«éäžããŠããŸãããã£ãã¯ã¿ã€ããŒå²ã蟌ã¿ãã³ãã©ãŒã§ã100ããªç§ééã®çµéã®ã€ãã³ããèšé²ããããã¯ãé¢æ°ADC_START_CONVïŒïŒã䜿çšããŠADå€æãéå§ãããŸããçŽ104ÎŒsã®ééã®åŸãADå€æã®çµæãæºåããããšãå²ã蟌ã¿ãããªã¬ãŒãããŸãã ISR_ADCïŒïŒãã³ãã©ãŒã®æ¬äœã§ã¯ãADå€æã®çµæãADC_Resultå€æ°ã«èšé²ãããboolåã®ADCReadyFlagæºåãã©ã°ãèšå®ãããŸãïŒãã®åã䜿çšããã«ã¯ãstdbool.hã©ã€ãã©ãªã#includeãã£ã¬ã¯ãã£ãã§ä»¥åã«æ¥ç¶ãããŠããŸããïŒã
ãã®ãã©ã°ãèšå®ããã€ãã³ãã¯ãifã¹ããŒãã¡ã³ãã«ãã£ãŠã¡ã€ã³ã«ãŒãã§ä¿®æ£ãããŸãããã®åŸããã©ã°ããªã»ãããããADå€æã®8ã€ã®çµæãåèšããããŸã§ãADå€æã®çµæADC_Resultãå€æ°Sumã«åèšãããŸãã 8ã€ã®çµæãååŸãããåèšããããšãSumå€æ°ã®åèšã8ã§é€ç®ãããŸãããããã£ãŠãã·ã³ãã«ãªããžã¿ã«ãã£ã«ã¿ãªã³ã°ãªãã·ã§ã³ãå®è£ ãããå€éšãã£ã«ã¿ãªã³ã°ã¹ããŒã ã«å ããŠãã€ãºã®å€ãUSBé»æºã«ããäœãã€ãºããããåæžãããŸãã
ãã£ã«ã¿ãªã³ã°ãããçµæã¯ADC_Resultå€æ°ã«æžã蟌ãŸããSumå€æ°ã¯ãŒãã«ãªã»ãããããŸãã次ã«ãéåã§åŸããããã£ã«ã¿ãªã³ã°çµæã¯ãåçŽãªç®è¡æŒç®ã䜿çšããŠãã«ãã«å€æãããŸãããªããªãåºæºé»å§ã¯5 Vã§ãããã«å¯Ÿå¿ããéåã®æ倧å€ã¯1023ã§ããéåã®å€ã®ãã«ãã«å€æããã«ã¯ãããã1023ã§é€ç®ãã5ã§ä¹ç®ããã ãã§ååã§ããçµæã®å°æ°å€ãå®æ°åã§ä¿åããŸããããã«ãTMP36ã«æå ã«ããŒã¿ã·ãŒããããã25床ã§ã¯åºåã750 mVã§ãããäžäžã¯10 mVã«çžåœããããšãããã£ãŠããããã次ã®åŒT =ïŒïŒT -0.75ïŒ* 100ïŒ+25ãããã«ã床åäœã®æž©åºŠå€ã¯void Seg_WriteïŒUINT TïŒé¢æ°ã䜿çšããŠ7ã»ã°ã¡ã³ãã€ã³ãžã±ãŒã¿ãŒã«è¡šç€ºãããŸããããã¯ãæåã®äŸãšã¯ç°ãªãã2æ¡ã®æ°åã床èšå·ã§è¡šç€ºããŸãããã£ã¹ãã¬ã€ã«è¡šç€ºãããæ°å幎ã®è«çã¯åãã§ãã
ãããã°äžã«ãèšè¿°ãããããã°ã©ã ã®ãã¹ãŠã®ããžãã¯ãããã³äžéå€æ°ã®å€ãšæçµçµæã®äž¡æ¹ã®èšç®ã®æ£ç¢ºãããã¬ãŒã¹ã§ããŸãã
ãããã«
IARãããžã§ã¯ãã®åœ¢åŒã®ãã¹ãŠã®äŸã¯ãããããããŠã³ããŒãã§ããŸãã