项目-avr程序实例megac1.参考书P121数码管轮流显示_第1页
项目-avr程序实例megac1.参考书P121数码管轮流显示_第2页
项目-avr程序实例megac1.参考书P121数码管轮流显示_第3页
项目-avr程序实例megac1.参考书P121数码管轮流显示_第4页
项目-avr程序实例megac1.参考书P121数码管轮流显示_第5页
已阅读5页,还剩33页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1.P121#include<mega16.h>#include<delay.h>flashunsignedcharled_7[16]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,flashunsignedcharposition[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};voidmain(void){unsignedchar {{{}}}#includeflashunsignedcharflashunsignedcharposition[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};unsignedchardisbuff[8];//显示缓冲区,存放要显示的8个字符的段码值unsignedcharposit,time_counter;bitvoid 6位LED{PORTC=PORTA=led_7[disbuff[posit]];PORTC=position[posit];if(++posit>=8)posit=}Timer0interrupt P] { LEDif{time_counter=time_500ms_ok=}}void{unsignedchar I/O//T/C0 //OCR0=0x7C(124),(124+1)/62.5=2ms(2ms中断一次,电平方向变化一次) //允许T/C0比较匹配中断posit=0; while{{if(++new>=16)new=0;}}}8,逐段、#include#includeflashunsignedcharled_7[8]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};flashunsignedcharposition[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};voidmain(void){unsignedchar {{{}}}#include<mega16.h>flashunsignedcharposition[2]={0xfe,0xfd};unsignedchar //unsignedchar //6int //unsignedcharposit; void 6LED{PORTC=PORTA=led_7[dis_buff[posit]];PORTC=position[posit];if(++posit>=2)posit=}Timer0interrupt P] { //调用LEDif{time_counter=time_1s_ok=}}void //{dis_buff[0]=time%10;dis_buff[1]=time/10;}void{ //I/OT/C0 //(M/64=62.5KHz,CC //= //T/C0posit= //while{if 1{time_1s_ok=iftime0)//{} //}}}数码管动态扫描0170 #include<mega16.h>#include<delay.h>flashunsignedcharflashunsignedchar void{while{unsignedintpoint=0,show=0,i=0,k=0,position2=7;unsignedintmax=8,count=50;chartime_counter=100;{{{{}}}}} #include<mega16.h>#include<delay.h>flashunsignedcharposition[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};unsignedchardisbuff[8];void{unsignedchar //I/O {{{{}}}}}}}#include<mega16.h>#include<delay.h>flashunsignedcharposition[2]={0xfe,0xfd};unsignedchar //6unsignedcharvoid{unsignedchari,j;{}}void{}interrupt[EXT_INT0]void{ interrupt[EXT_INT1]void{if(num)num--;elsenum=99;}void //允许INT0、INT1中断 //INT0、INT1下降沿触发 //清除INT0、INT1中断标志位 //全局中断允许while{}}#include<mega16.h>#include<delay.h>flashunsignedcharposition[2]={0xfe,0xfd};unsignedchar //6unsignedcharvoid 6LED{PORTC=PORTA=led_7[dis_buff[posit]];PORTC=position[posit];if(++posit>=2)posit=Timer0interrupt{P]//调用LED}void{}interrupt[EXT_INT0]void{}interrupt[EXT_INT1]void{if(num)num--;elsenum=99;}void //允许INT0、INT1中断 //INT0、INT1下降沿触发 //清除INT0、INT1中断标志位T/C0 //(M/64=62.5KHz,CC //= //T/C0 //while{}}#include<mega16.h>flashunsignedcharled_7[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};flashunsignedcharposition[2]={0xfe,0xfd};unsignedcharcounter,time_10ms_counter=0;unsignedchardisbuffer[2],i=0;bitvoid }interrupt P] { { }}void{ }#definekey_input7PIND.7#definekey_input6PIND.6#definekey_state_00#definekey_state_1#definekey_state_2unsignedchar{staticunsignedcharkey_state7=0;unsignedcharkey_press7,key_return7=0;{casecase {}case}return}unsignedchar{staticunsignedcharkey_state6=0;unsignedcharkey_press6,key_return6=0;{casecase{ }case}return}void while{ {{ }{ elsecounter=99;} }}#include#definerow#definespeed flashunsignedcharchar_7[row]={0x10,0x38,0x7C,0xFE,0x38,0x38,0x38,0x38,0x00,0x00,0x00,0x00};bittime_1ms_ok;unsignedchardis_buff[8];voiddisplay(void){staticunsignedcharPORTC=PORTA=PORTC=if(++i>=8)i=}interrupt P] 1ms{display();time_1ms_ok=1;}void{unsignedcharmove_speed,i,j; T/C0CTC for(i=0;i<=8;i++){dis_buff[i]=char_7[i];}j=8;while{if{time_1ms_ok=if+move_speed>= {move_speed=0; move_speed,则依次向上移动一行for(i=0;i<=6;i++){dis_buff[i]=}dis_buff[7]=char_7[j];if(++j>=row)j=0;}}}}#include<mega16.h>flashunsignedchartab[8][4]= flashunsignedcharunsignedchark=0;unsignedcharj=0,m=0;inttime_1s_counter=0;bittime_1s_ok=0;voiddisplay(unsignedcharelsem=i/8+1; }interrupt P] { { }}//Declareyourglobalvariablesherevoidmain(void) while(1){ { }}书P269使用定时器的产生特定频率,输出到蜂鸣器上产生不同的音调。使用定时器产生DOREMIFASOLA声音,每个音长1秒。(假设do256re288mi320fa341so384la426si480hz#include<mega16.h>flashunsignedintt[9]=flashunsignedchard[9]={0,105,116,132,140,157,176,198,209};#defineMax_note flashunsignedcharmusic[Max_note]=unsignedcharnote_n;unsignedintint_n;bitplay_on;//ExternalInterrupt1serviceroutineinterrupt[EXT_INT1]voidext_int1_isr(void){if{TCCR1B=}}//Timer1outputcompareAinterruptserviceroutineinterrupt[ PA]void {if{note_n=int_n=play_on=}{if(--int_n=={TCCR1B=if(note_n<{OCR1A=4*t[music[note_n]];int_n=d[music[note_n]];int_n=int_n*music[note_n];TCCR1B=}play_on=}}}//Declareyourglobalvariablesvoid{TCNT1=0x00;ICR1H=0x00;ICR1L=0x00;OCR1A=0x00;OCR1B=//Timer(s)/Counter(s)Interrupt(s)initialization//ExternalInterrupt(s)//INT0://INT1://INT1Mode:Falling//INT2:Off//Globalenableinterruptswhile{}}设计一个简单系统,有2个按键和8个LED数码管作为显示。两个按键分别代表输入字符“a”和“b”,且具备类似PC机键盘的连发功能:如按下“a”键不放,第1秒内输入1“a”,以后每隔0.2“a”……。输入的“a”在8LED#includeflashunsignedcharflashunsignedcharposition[8]=0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};unsignedchardis_buff[8];//显示缓冲区,存放要显示的8个字符的段码值unsignedcharunsignedcharkey_stime_counter;bitkey_stime_ok;void 8位LED{PORTC=PORTA= PORTC=if(++posit>=8)posit= //}Timer0比较匹配中断服务,2msinterruptinterrupt P] { LEDif(++key_stime_counter key_stime_counter= key_stime_ok 10ms}#define //按键 #definekey_input6 #definekey_state_00#definekey_state_1#definekey_state_2#definekey_state_3unsignedchar{staticunsignedcharkey_state7=0,key_time=unsignedcharkey_press,key_return7=key_press 读按键I/Oswitch case if(!key_presskey_state7key_state_1 case if key_state7key_state_2;按键仍按下,状态转换到计时 key_time key_state7 case if key_state7key_state_0; key_return7 输出 elseif(++key_time key_state7 key_time key_return7 输出 case if key_state7key_state_0 if(++key_time>={{输出输出key_return7=key_time=}} return}unsignedchar{staticunsignedcharkey_state6=0,key_time=unsignedcharkey_press,key_return6=key_press 读按键I/Oswitch case if(!key_presskey_state6key_state_1 case if key_state6key_state_2按键仍按下,状态转换到计时 key_time key_state6 case if key_state6key_state_0; key_return6 输出 elseif(++key_time key_state6 key_time key_return6 输出 case if key_state6key_state_0 if(++key_time key_time key_return6 输出 return}void{ unsignedcharPORTA DDRA=PORTC=DDRC=DDRD PD7、PD6T/C0TCCR0 TCNT0=OCR0= //OCR0=TIMSK posit= while if key_stime_ok 10ms switch case case }} switch case case if(n>=8) }#include<mega16.h>flashunsignedcharflashunsignedcharposition[8]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};unsignedcharposit;unsignedchar bitunsigned void 8位LED{PORTC=PORTA=led_3[dis_buff[posit]];PORTC=position[posit];if(++posit>=8)posit=}Timer0,2msinterrupt P] { LED扫描显示if(++key_stime_counter>=5){key_stime_counter=key_stime_ok 10ms}}#definekey_input_a //按键输#definekey_input_bPIND.1 //按键输b#definekey_state_00#definekey_state_1#definekey_state_2#definekey_state_3unsignedchar{staticunsignedcharkey_state=0,key_time=0;unsignedcharkey_press,key_return=0;key_press a的I/Oswitch{case if(!key_presskey_statekey_state_1;case if{key_state=key_state_2;//按键仍按下,状态转换到计时1key_time=0; //清另按键时间计数器}key_statekey_state_0;casekey_state_2:if(key_press){key_statekey_state_0;key_return }elseif(++key_time {key_state=key_state_3;//按下时间>1s,状态转换到计时2key_time=0; //清按键计数器key_return }casekey_state_3:if(key_press)key_statekey_state_0;{if(++key_time {key_time >0.2s,key_return }}}return}unsignedchar{staticunsignedcharkey_state=0,key_time=0;unsignedcharkey_press,key_return=0;key_press b的I/Oswitch{case if(!key_presskey_statekey_state_1;case if{key_state=key_state_2;//按键仍按下,状态转换到计时1key_time=0; //清另按键时间计数器}key_statekey_state_0;casekey_state_2:if(key_press){key_statekey_state_0;key_return }elseif(++key_time {key_state=key_state_3;//按下时间>1s,状态转换到计时2key_time=0; //清按键计数器key_return }casekey_state_3:if(key_press)key_statekey_state_0;{

if(++key_time {key_time >0.2s,key_return }}}return}void{unsignedcharPORTA I/ODDRA=PORTC=DDRC=DDRD= ////T/C0 //OCR0= T/C0for(i=0;i<8 //LED初始显示8个"" //开放全局中断while{if{key_stime_ok=0; 10ms到switch(read_key_n_a()){casefor(i=0;i<7;{dis_buff[i]=dis_buff[i+1];} //LED显示左移一位dis_buff[7]=1; //最右显示acase2:for(i=0;i<7;{dis_buff[i]=dis_buff[i+1];} //LED显示左移一位dis_buff[7]=1; //最右显示a}switch{casefor(i=0;i<7;{dis_buff[i]=dis_buff[i+1];} //LED显示左移一位dis_buff[7]=2; //最右显示bcase2:for(i=0;i<7;{dis_buff[i]=dis_buff[i+1];} //LED显示左移一位dis_buff[7]=2; //最右显示b}}}}个作为数字按下两次,将LED数码管内容清为0。初始状态显示0。 #include<mega16.h>#defineucharunsignedchar#definenokey255flashucharflashucharposition[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};uchardisbuff[8];ucharposit=0;unsignedlongintresult[3];ucharkeynow; uchartime_10ms_counter;bittime_10ms_ok;ucharreset=0,i,j,k=0;bitplus=0,cleardis=0,clearres=0;voiddisplay() interruptinterrupt P] uchar{staticucharkeystate=0,keyvalue,keyline;ucharkeyreturn=nokey; case case case case case casecasecasecasecasecasecasecasecase}}}}casecase}}returnreturn}}void{ 该是while(1) {{}}}}{{}}}}}}#include<mega16.h>#defineucharunsignedchar#definenokey255flashucharled_7[13]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x00};flashucharposition[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};uchardisbuff[8];ucharposit=0;unsignedlongintresult[3];ucharkeynow;uchartime_10ms_counter;bittime_10ms_ok;ucharbitplus=0,cleardis=0,clearres=0;voiddisplay() }interrupt P] {{ }}uchar{staticucharkeystate=0,keyvalue,keyline;ucharkeyreturn=nokey;{case{case1:

}

{}{{case0b case0b case0b case0b case0b case0b case0b case0b case0b case0b case0b case0b }}case2:}

return}void 该是while(1){{ {

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论