📄 feedis_uart._c
字号:
#include <include.h>
#pragma interrupt_handler UART_RX_interrupt:iv_USART0_RX
#pragma interrupt_handler int0_isr:iv_INT0
//全局变量
//如果变量会在中断服务程序中被修改,须加volatile限定
volatile unsigned char FLAG = 0; //按键标志
volatile unsigned char RX_index; //存放接收数据的个数
volatile uchar ready_rx=0;
volatile uchar receivebox[70];
unsigned char light;
extern uchar hantemp[32];
//extern uchar dis[1024];
extern uchar dis[3072];
extern volatile uchar ISDstatus ;
//extern volatile uchar countwait;
extern uchar ISDmoney[5];
volatile uchar SPEAK ;
zhx zx={0x38,
0x38,0x38,0x38,0x38,
0x38,0x38,0x38,0x38,
0x38,0x38,0x38, 0x38};
uchar flg;
uchar dip=0;
void int0_isr(void)
{
//put_c(0xa5);
//countwait = 0;
SPEAK = 1;
//if(ISDstatus & 0X02)
// ISDstatus &= 0xfd;
}
void put_c(unsigned char c) //发送采用查询方式
{
while( !(UCSR0A & (1<<UDRE0)) );
UDR0=c;
}
void put_s(unsigned char *ptr)
{
while (*ptr)
{
put_c(*ptr++);
}
put_c(0x0D);
put_c(0x0A); //结尾发送回车换行
}
void UART_RX_interrupt(void) //串口接收中断服务程序
{
uchar temp;
temp = UDR0;
// countwait = 0;
switch(ready_rx)
{
case 0:
if(temp == 'D' ) //如果接收到显示控制命令
{
FLAG = 0;
ready_rx = 1;
RX_index = 1;
receivebox[0] = temp;
}
else if(temp == 'A') // 接收到A打头控制命令
{
FLAG = 0;
ready_rx = 2;
receivebox[0] = temp;
}
else if(temp ==0x43)
{
clean(0);
}
break;
case 1:
receivebox[1] = temp; //存下亮度值
RX_index = 2;
ready_rx = 3;
break;
case 2:
if(temp=='a')
{ready_rx=5;receivebox[1]|=0xf0;RX_index = 14;}
/*else if(temp=='b')
{ready_rx=6;receivebox[1]|=0x20;}
else if(temp=='c')
{ready_rx=7;receivebox[1]|=0x40;}
else if(temp=='d')
{ready_rx=8;receivebox[1]|=0x80;}*/
else {ready_rx=10;receivebox[1] = temp; RX_index = 5;}
break;
case 3:
receivebox[2] = temp; //存下位置值
if(temp==0x00)
{
RX_index = 64;
ready_rx = 4;
}
else if((temp>0)&&(temp<5))
{ RX_index = 16;
ready_rx = 4;
}
else
{ ready_rx = 0;
}
break;
case 4:
if(receivebox[2] ==0x00)
{receivebox[64 - RX_index + 3] = temp;
}
else
{receivebox[16 - RX_index + 3] = temp;
}
RX_index--;
//if(!(c) || (temp == 0x05))
// ready_rx = 0;
break;
case 5:
RX_index--;
if(RX_index==13)
{zx.zhou=temp;flg|=0x10;}
else if((RX_index>8)&&(RX_index<13))
{ zx.dunwei[12-RX_index]=temp;flg|=0x20;}
else if((RX_index>4)&&(RX_index<9))
{zx.zongzhong[8-RX_index]=temp;flg|=0x40;}
else if((RX_index>0)&&(RX_index<5))
{zx.chaoxian[4-RX_index]=temp;flg|=0x80;}
break;
case 10:
RX_index--;
receivebox[5 - RX_index + 1] = temp;
if(RX_index==0) flg=0xf0;
//if(!(RX_index--) || (temp == 0x05))
// ready_rx = 0;
break;
default:
ready_rx = 0;
RX_index = 0;
break;
}
// 如果收不满16或者64字节数据,最后停止位是 0x05
if ( (ready_rx ==4) && (RX_index == 0) )
{ //receivebox[65]=0x05;
ready_rx = 0;
FLAG = 1;
//uart0_txd(0xaa);
}
if((RX_index == 0) &&(( ready_rx >= 5)&&( ready_rx <= 10) ))
{
ready_rx = 0;
if(flg==0xf0)
{flg=0;
FLAG=1;
}
}
}
void uart0_txd(uchar x)
{ while((UCSR0A&0X20)==0);
UDR0=x;
}
void init_USART(void)//USART 初始化
{
//USART 9600 8, n,1 PC上位机软件(超级终端等)也要设成同样的设置才能通讯
UCSR0B = 0x00; //disable while setting baud rate
UCSR0A = 0x00;
UCSR0C = 0x06;
//异步,8位数据,无奇偶校验,一个停止位,无倍速
UBRR0L= (F_CPU/BAUDRATE/16-1)%256;
UBRR0H= (F_CPU/BAUDRATE/16-1)/256;
UCSR0B = (1<<RXCIE0)|(1<<RXEN0)|(1<<TXEN0);
//使能接收中断,使能接收,使能发送
}
void port_init(void)
{
PORTA = 0x00;
DDRA = 0xff;
PORTB = 0xAD;
DDRB = 0xF7;
PORTC = 0xFF; //m103 output only
DDRC = 0xFF;
PORTD = 0x05;
DDRD = 0xFA;
PORTE = 0xFE;
DDRE = 0xFE;
PORTF = 0x0F;
DDRF = 0x0F;
PORTG = 0x1F;
DDRG = 0x0F;
}
void timer1_init(void)
{
TCCR1B = 0x00; //stop
TCNT1H = 0xFF; //setup
TCNT1L = 0x0C;
OCR1AH = 0x00;
OCR1AL = 0xF4;
OCR1BH = 0x00;
OCR1BL = 0xF4;
OCR1CH = 0x00;
OCR1CL = 0xF4;
ICR1H = 0x00;
ICR1L = 0xF4;
TCCR1A = 0x00;
TCCR1B = 0x05; //start Timer
}
void spi_inith(void)
{// DDRB|=(1<<0)|(1<<1)|(1<<2);//ss sck mosi
// PORTB|=(1<<0)|(1<<1)|(1<<2);
DDRB|=(1<<0)|(1<<1)|(1<<2);
PORTB|=(1<<0)|(1<<1)|(1<<2);
SPCR=0X70; //低先发
// SPCR=0X50; //164高先发
SPSR|=(1<<SPI2X);//倍速度
}
int main(void)
{
uchar temp;
uint temp1;
uint discounthh=0;
uchar *ptr;
uchar flag_control=0;
CLI(); //disable all interrupts
port_init();
spi_inith();
FLAG=0;
ISDstatus = 0;
SPEAK = 1;
// countwait = 0;
init_USART();
EICRA = 0x02; //extended ext ints falling edge
EICRB = 0x00; //extended ext ints
EIMSK = 0x01;
SEI(); //使能全局中断
waitms(10);
light = RTEEPROMread(12);
if(light>56)light=56;
//spi_init();
///////////////////////////////////////
FLAG=1;
receivebox[0]='A';
receivebox[1]=0XF0;
ISDstatus=0;
SPEAK = 1;
//for(temp1=0;temp1<3072;temp1++)
//dis[temp1]=0xff;
///////////////////////////////////////
// uart0_txd(0xaa);
dip=0;
while (1)
{
if(FLAG)//串口中断收到有效数据FLAG为1
{
hanzi(); //为显示准备好点阵数据
FLAG = 0;
}
ledscreendis(light);//显示屏扫描程序
ledscreendis(light);//显示屏扫描程序
if(flag_control==0)
{dip++;
if(dip==129)
dip=128;
}
else if(flag_control==1)
{dip--;
if(dip==127)
dip=128;
}
discounthh++;
if(discounthh==700)
{uart0_txd(light);
//u
dip=0;
discounthh=0;
if(flag_control==0)flag_control=1;
else if(flag_control==1)flag_control=0;
}
if(ISDstatus>0)
{
if(SPEAK==1)
{ switch (ISDstatus)
{ case 0x06:
ISDplay(34+ISDmoney[0]-0x30);
SPEAK=0;
break;
case 0x05:
ISDplay(20);
SPEAK=0;
break;
case 0x04:
if(ISDmoney[1]!=0x20)
{ISDplay(82+ISDmoney[1]-0x30);
SPEAK=0;
}
break;
case 0x03:
if(ISDmoney[2]!=0x20)
{if(ISDmoney[2]!=0x30)
{ISDplay(73+ISDmoney[2]-0x30);SPEAK=0;}
else
{if((ISDmoney[3]!=0x30)||(ISDmoney[4]!=0x30))
{ISDplay(54);SPEAK=0;}
}
}break;
case 0x02:
if(ISDmoney[3]!=0x20)
{if(ISDmoney[3]!=0x30)
{ISDplay(64+ISDmoney[3]-0x30); SPEAK=0;}
else
{
if((ISDmoney[4]!=0x30)&&(ISDmoney[2]!=0x30))//hou mian bu wei 0
{ISDplay(54); SPEAK=0;} // bo fang 0
}
}
break;
case 0x01:
ISDplay(44+ISDmoney[4]-0x30);
SPEAK=0; break;
default : break;
}
ISDstatus--;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -