|
把程序发上来,第一次写,大家嘴下留情,知道的帮看看。
平时一点问题没有,就是在工控机“开关机”的时候有时会出现我说的现象,也不是每次“开关机”都出现。
#include
#include
unsigned char temp,check,datadisp[4];
unsigned char num_code[20]={0XBF,0X86,0XDB,0XCF,0XE6,0XED,0XFD,0X87,0XFF,0XEF};
unsigned char receivedata[5];
unsigned char counter= 0;
unsigned char light;
void delay_ms(void)
{
unsigned int cnt;
for(cnt=0;cnt<255;cnt++);
}
void port_init(void)
{
PORTA = 0xFC;
DDRA = 0x00;
PORTB = 0x00;
DDRB = 0xFF;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0xFF;
DDRD = 0xFF;
}
void uart0_init(void)
{
UCSRB = 0x00; //disable while setting baud rate
UCSRA = 0x00;
UCSRC = BIT(URSEL) | 0x26;
UBRRL = 0xF9; //set baud rate lo
UBRRH = 0x00; //set baud rate hi
UCSRB = 0x90;
}
#pragma interrupt_handler uart0_rx_isr:12
void uart0_rx_isr(void)
{
//uart has received a character in UDR
unsigned char i;
check = UCSRA & 0X04;
if(!check)
{
temp = UDR;
if(temp == 0x0c)
{
counter = 0;
receivedata[counter] = temp;
counter++;
}
else if((temp<=9)&&receivedata[0]==0x0c)
{
receivedata[counter]= temp;
counter++;
}
else
counter = 0;
if(counter == 5)
{
datadisp[0] = receivedata[1];
datadisp[1] = receivedata[2];
datadisp[2] = receivedata[3];
datadisp[3] = receivedata[4];
for(i=0;i<5;i++)
{
receivedata=0x00;
}
}
}
}
void timer2_init(void)
{
TCCR2 = 0x00; //stop
ASSR = 0x00; //set async mode
TCNT2 = 0x01; //setup
OCR2 = 0xFF;
TCCR2 = 0x69; //start
}
void spi_init(void)
{
SPCR = 0x50; //setup SPI
SPSR = 0x00; //setup SPI
}
void pwm(void)
{
unsigned char switch1,switch2;
switch1 = PINA & 0X01;
switch2 = PINA & 0X02;
if((switch1==0)&&(switch2==0))
light = 0Xdf;
else if((switch1==0)&&(switch2!=0))
light = 0X9f;
else if((switch1!=0)&&(switch2==0))
light = 0X4f;
else
light = 0X0f;
}
void display(void)
{
unsigned char pp,p,q,j;
for(pp=0;pp<4;pp++)
{
p = datadisp[pp];
q = num_code[p] ;
OCR2 = 0XFF;
//CLEAR 595reg
PORTB &= ~BIT(6);
delay_ms();
PORTB |= BIT(6);
//end clear 595reg
SPDR = q;
while(!(SPSR & 0X80));
PORTB &= ~BIT(4);
delay_ms();
PORTB |= BIT(4);
OCR2 = light;
PORTB |= BIT(pp);
for(j=0;j<2;j++)
delay_ms();
PORTB &= 0Xf0;
}
}
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
uart0_init();
spi_init();
timer2_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x00; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
void main(void)
{
init_devices();
pwm();
while(1)
{
display();
}
} |
|