本帖最后由 ddllxxrr 于 2014-9-8 08:37 编辑
自发自收,就是用一个按键每键一个加1把这个数发出,自己中断收,再在显示屏上显示数字。
以下是编译通过的截图:
以下是程序:
- /*
- * GccApplication14.c
- *
- * Created: 2014-9-5 19:14:17
- * Author: Administrator
- */
- #include <avr/io.h>
- #include <avr/interrupt.h>
- #define uchar unsigned char
- #define uint unsigned int
- //定义LED与端口的连接
- #define c1 4//PORTD.4
- #define c2 5//PORTD.5
- #define button 2//PORTD.2
- #define UDRE 5
- #define FE 4
- #define PE 2
- #define DOR 3
- uchar count;
- uchar cnt,cnt1;
- uchar counth,countl;
- uchar tab[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8, //共阳极LED 0~F的段码
- 0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E};
- void delay(void) //LED数码管切换时间函数
- {
- uint i;
- for(i=0;i<10;i++);
- }
- void display(void) //LED数码管显示函数
- {
- counth=count/10; //十位分离
- countl=count%10; //个位分离
- PORTC=tab[counth]; //送十位的段码值到PORTC
- PORTD |= _BV(c1); //显示LED数码管的十位
- delay(); //保持一定时间
- PORTD &=~_BV(c1); //不显示LED数码管的十位
- PORTC=tab[countl]; //送个位的段码值到PORTC
- PORTD |= _BV(c2); //显示LED数码管的个位
- delay(); //保持一定时间
- PORTD &=~_BV(c2); //不显示LED数码管的个位
- }
- //interrupt [EXT_INT0] void int0_isr(void)//按键次数统计
- ISR(INT0_vect)
- {
- if (cnt==20)
- {
- cnt=0;
- }
- else
- {
- cnt++;
- }
- }
- //interrupt [USART_RXC] void usart_rx_isr(void)//USART串行接收中断
- ISR(USART_RXC_vect)
- {
- uchar status,data;
- status=UCSRA;
- data=UDR;
- if((status&((1<<FE)|(1<<PE)|(1<<DOR)))==0)
- {
- count=data;
- }
- display();
- }
- void USART_Transmit(char dat)
- {
- while( !(UCSRA & (1 << UDRE)) ); //只有数据寄存器为空时才能发送数据
- UDR = dat;
- }
- int main(void)
- {
- DDRC=0xFF;
- PORTC=0xFF;
- DDRD=0xFA;
- PORTD=0xFF;
- UCSRA=0x00;
- UCSRB=0x98;
- UCSRC=0x86;
- UBRRH=0;
- UBRRL=25; //系统时钟8MHz,波特率为9600bps
- MCUCR=0x02; //INT0为下降沿时产生中断请求
- GICR=0x40; //允许INT0产生中断
- //#asm("sei")
- sei();
- while(1)
- {
- USART_Transmit(cnt1);
- cnt1=cnt;
- }
- }
|