There is something wrong with the program, please solve it[Copy link]
The course has been designed for a week, and I have encountered many problems with the program and schematic diagram. The following three have not been solved yet. Please give me some advice and save the children! Please solve these three problems: Countdown does not work, Emergency mode does not respond, Modify time does not respond
#include<iom168v.h>
const unsigned char disp[] ={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90}; //segment selection codeunsigned char xs[4]= {0xC0,0xC0,0xC0,0xC0}; unsigned char gs=0; //green lightunsigned char ys=0; //yellow lightunsigned char green_second = 9 , yellow_second = 3 ; //variable used to modify timeunsigned char k=0; //display refresh flagunsigned char flag=0; //switch traffic light flagunsigned char flag1=0; //switch traffic light mode flagunsigned char flag2=0; //switch mode in emergency modeunsigned char flag3=0; //modify timeunsigned int Udelay = 1000; //The main frequency is 1M void delay_1us(void) //1us delay, actually measured 8us { asm("nop"); //1 clock cycle } void delay_3us(void) //3us, actually 8us { asm("nop"); //1 clock cycle } void delay_10us(void) //10us, actually 9us { asm("nop"); asm("nop"); } void delay_50us(void) //50us, actually 47us { delay_10us(); delay_10us(); delay_10us(); delay_10us(); delay_10us(); } void delay_100us(void) //100us, actually 96us { delay_50us(); delay_50us(); } void delay_n100us(unsigned char n100us) //Several hundred us delay { while(n100us--); delay_100us(); } //Delay function void delay(unsigned int x) { while(x--); } //IO configuration function void io_init(void) { DDRD = 0xFF; PORTD = 0xFF; DDRB = 0b00111111; PORTB = 0xFF; DDRC = 0b00111100; PORTC = 0b00111111; PORTC|=0b00111100;//4-bit control always unchanged} //EEPROM write, read void EEPROM_WRITE(unsigned int uiAddress, unsigned char ucData) { while(EECR & (1<<eewe)); %="" &="" &&="" ((flag1="=" ((pinb="" (1<<eewe));="" )="" *,="" *break;="" *eedr*="" *switch(b)="" *void="" +="" +1="" -1="" 0="" 0)="" 0)||(flag1="=" 0x01)="=" 0x02)="=" 0x80)="=" 0xff;="" 0xff;xs[2]="0xff;xs[3]" 1="" 1)="" 10];="" 3))="" 3;="" :*="" ;="" =="" a="" b*="" case="" changetime(void)="" char="" decode(unsigned="" delay(40000);="" delay(50000);="" delay_n100us(100);="" eear="uiAddress;//设置地址和数据寄存器" eecr="" eedr="ucData;" eeprom_read(unsigned="" eeprom_write(0x00,="" flag1="0;" flag3="(flag3" green_second);="" green_second++;="" if(="" if((pinc="" if(flag3="=" if(green_second="" int="" portb="" portc|="0b00111100;" return="" shutdownled(void)="" tccr0b="0x00;" tccr1b="0x00;" uiaddress)="" unsigned="" void="" while(eecr="" xs[0]="disp[a" xs[1]="disp[a" xs[2]="disp[a" xs[3]="disp[a" {="" |="(1<<EEMWE);" }="" }*="" 以启动写操作e="" 以启动读操作="" 修改时间="" 关位选="" 关闭显示清空段选段选="" 关闭显示灯="" 只能在普通模式下改变时间="" 等待上一次写操作结束="" 绿灯闪="" 置位eemwe="" 自数据寄存器返回数据="" 设置eere="" 译码函数="">0) { green_second--; EEPROM_WRITE(0x00, green_second); delay(40000); } } } //Digital tube 2 displays green light time decode(green_second/*, 1*//*); } if(flag3 == 2)//2 yellow light { //Yellow light flashes PORTB &= ~0x12; if((PINC & 0x01) == 0) //+1 { delay_n100us(100); if((PINC & 0x01) == 0) { yellow_second++; EEPROM_WRITE(0x02, yellow_second); UDR0 = 0x02; delay(10000); UDR0 = yellow_second; delay(40000); } } if((PINC & 0x02) == 0) //-1 { delay_n100us(100); if((PINC & 0x02) == 0) { if(yellow_second > 0) { yellow_second--; EEPROM_WRITE(0x02, yellow_second); UDR0 = 0x02; delay(10000); UDR0 = yellow_second; delay(40000); } } } //Digital tube 2 displays yellow light time decode(yellow_second/*, 1*//*); } }*/ //Traffic light display function void led(void) { switch(flag1) { case 0: {//Daytime mode TCCR0B = 0x03; TCCR1B = 0x0b; switch(flag) { case 0: xs[2]= 0xbf; xs[3]= 0xbf; decode(gs/*, 0*/); PORTB |= 0x08; //Turn off the left and right red lights PORTB |= 0x02; //Turn off the upper and lower yellow lights PORTB &= ~0x20; //Turn on the left and right green lights PORTB &= ~0x01; //Turn on the upper and lower red lights break; case 1: decode(ys/*, 0*/); PORTB &= ~0x01; //Turn on the upper and lower red lights PORTB |= 0x20; //Turn off the left and right green lights PORTB &= ~0x10; //Turn on the left and right yellow lights if(ys <= 3) { decode(ys/*, 1*/); } break; case 2: xs[0]= 0xbf; xs[1]= 0xbf; decode(gs/*, 1*/); PORTB |= 0x10; //Turn off the left and right yellow lights PORTB |= 0x01; //Turn off the upper and lower red lights PORTB &= ~0x04; //Turn on the upper and lower green lights PORTB &= ~0x08; //Turn on the left and right red lights break; case 3: decode(ys/*, 1*/); PORTB &= ~0x08; //Turn on the left and right red lights PORTB |= 0x04; //Turn off the up and down green lights PORTB &= ~0x02; //Turn on the up and down yellow lights if(ys <= 3) { decode(ys/*, 0*/); } break; } break;} case 1: //Emergency mode { PORTB |= 0x3f; shutdownLED(); switch(flag2) { case 0://Emergency mode 1 { PORTB &= ~0x08;//Turn on the left and right red lights PORTB &= ~0x01; //Turn on the up and down red lights break; } case 1://Emergency mode 2 { PORTB &= ~0x20; //Turn on the left and right green lights PORTB &= ~0x01; //Turn on the up and down red lights break; } case 2://Emergency mode 3 { PORTB &= ~0x04;//Turn on the up and down green lights PORTB &= ~0x08;//Turn on the left and right red lights break; } //break;//Added by myself } break; } case 2://Night mode { PORTB |= 0x3f; shutdownLED(); flag2 = 0; PORTB &= ~0x02;//Turn on the upper and lower yellow lights PORTB &= ~0x10;//Turn on the left and right yellow lights delay(50000); PORTB |= 0x02;//Turn off the upper and lower yellow lights PORTB |= 0x10;//Turn off the left and right yellow lights delay(50000); break; } case 3: { TCCR1B = 0x00; //Turn off timing xs[0] = 0xbf; xs[1] = 0xbf; break; } } } //Main function void main(void) { gs = green_second; ys = yellow_second; io_init(); //IO port initialization//Timer initialization timer0_init(); timer1_init(); SREG|=0x80; //Turn on total interrupt//Data write to EEPROM EEPROM_READ(0x00/*,green_second*/);delay(10); EEPROM_READ(0x02/*,yellow_second*/);delay(10); if(green_second>50) EEPROM_WRITE(0x00, gs); if(yellow_second>50) EEPROM_WRITE(0x02, ys); EEPROM_READ(0x00/* green_second*/);delay(10); EEPROM_READ(0x02/* yellow_second*/);delay(10); gs = green_second; ys = yellow_second; delay(10000); while(1) { //Modify mode if( ((PINB & 0x40) == 0) && (flag3 == 0)) { PORTB |= 0x3f; flag1 = (flag1 + 1 ) % 3; delay(20000); } //Switch in emergency mode if( ((PINB & 0x80) == 0) && (flag1 == 1)) { PORTB |= 0x3f; flag2 = 0; delay(20000); } //East-West direction if( ((PINC & 0x01) == 0) && (flag1 == 1)) { PORTB |= 0x3f; flag2 = 1; delay(20000); } //North-South direction if( ((PINC & 0x02) == 0) && (flag1 == 1)) { PORTB |= 0x3f; flag2 = 2; delay(20000); } //led display function led(); //Modify time function //changetime(); } } // Initialization of timer 0 void timer0_init(void) { TCCR0A = 0x02; //CTC mode TCCR0B = 0x03; //64 division OCR0A = 78; //5ms 78.125 TIMSK0 = 0x02;/*Compare interrupt A enabled*/ TIFR0 = 0x02; } //Timer 1 initialization void timer1_init(void) { TCCR1A = 0x00; //CTC mode TCCR1B = 0x0b; //64 division OCR1A = 15625; //1s = 15625 TIMSK1 = 0x02; /*Compare interrupt A enabled*/ TIFR1 = 0x02; } extern unsigned char k; extern unsigned char xs[4]; extern unsigned char green_second, yellow_second; extern unsigned char gs; extern unsigned char ys; extern unsigned char flag; extern unsigned char flag1; //Switch traffic light mode flag extern unsigned char flag2; //Switching mode in emergency mode extern unsigned char flag3; //Modification time #pragma interrupt_handler Int_TCCR1A:12 void Int_TCCR1A(void) { switch(flag) { case 0 : gs--; //decode(gs);//zi+ ys = yellow_second; if(gs == 0) { flag = 1; } break; case 1 : ys--; //decode( ys); //+ gs = green_second; if(ys == 0) { flag = 2; } break; case 2 : gs--; //decode(gs); //+ ys = yellow_second; if(gs == 0) { flag = 3; } break; case 3 : ys--; //decode(ys);//+ gs = green_second; if(ys == 0) { flag = 0; } break; } } // Function of interrupt service routine: refresh segment code and bit control, use variable K to achieve refresh in turn #pragma interrupt_handler Int_TCCR0A:15 void Int_TCCR0A(void) { k=(++k)%4; PORTC|=0b00111100;//turn off bit selection PORTD=xs[4]; switch(k) { case 0:PORTC &= ~0x04; //PORTD &= ~0x04;//Display thousands break; case 1: PORTC &= ~0x08; //PORTD &= ~0x08;//Display hundreds break; case 2: PORTC &= ~0x10; //PORTD &= ~0x80;//Display tens break; case 3: PORTC &= ~0x20; //PORTC &= ~0x08;//Display ones break; default: ; } }
bin\iccavr -e -D__ICC_VERSION=722 -D_EE_EXTIO -DATMega1280 -l -g -MLongJump -MHasMul -MEnhanced -Wf-use_elpm -c Traffic light ATMEGAL16.c
!WF:\New0170~1\Traffic light ATMEGAL16.c(325): [warning] calling a function without prototype may cause runtime errors if the function
does not return int or unsigned int
!WF:\新0170~1\TRAFFIC LIGHTATMEGAL16.c(326):[warning] calling a function without prototype may cause runtime errors if the function
does not return int or unsigned int
!WF:\新0170~1\TRAFFIC LIGHTATMEGAL16.c(384):[warning] declaration of `timer0_init' does not match previous declaration at F:\新0170~1\交绿灯ATMEGAL16.c(325)
!WF:\新0170~1\交绿灯ATMEGAL16.c(395):[warning] declaration of `timer1_init' does not match previous declaration at F:\新0170~1_Traffic lightATMEGAL16.c(326)
Done. Thu Nov 21 14:01:25 2019
Details
Published on 2019-11-21 14:03
For this kind of problem, we should find a problem step by step, annotate each function one by one, if the countdown is abnormal, we should find out whether the timer is working properly, whether the display function is normal, etc. If the mode cannot be switched, is it a button judgment problem, or a conflict in logic? In most cases, we can locate the approximate problem by figuring out a little bit, and then think of a solution.
bin\iccavr -e -D__ICC_VERSION=722 -D_EE_EXTIO -DATMega1280 -l -g -MLongJump -MHasMul -MEnhanced -Wf-use_elpm -c Traffic light ATMEGAL16.c
!WF:\New0170~1\Traffic light ATMEGAL16.c(325): [warning] calling a function without prototype may cause runtime errors if the function
does not return int or unsigned int
!WF:\新0170~1\TRAFFIC LIGHTATMEGAL16.c(326):[warning] calling a function without prototype may cause runtime errors if the function
does not return int or unsigned int
!WF:\新0170~1\TRAFFIC LIGHTATMEGAL16.c(384):[warning] declaration of `timer0_init' does not match previous declaration at F:\新0170~1\交绿灯ATMEGAL16.c(325)
!WF:\新0170~1\交绿灯ATMEGAL16.c(395):[warning] declaration of `timer1_init' does not match previous declaration at F:\新0170~1_Traffic lightATMEGAL16.c(326)
Done. Thu Nov 21 14:01:25 2019