基于PIC18F45K22的几个小程序

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

01闪烁的LED(16MHz)/*名称:闪烁的LED说明:LED按设定的时间间隔闪烁*/#includep18cxxx.h//#pragmaconfigFOSC=HSMP,PLLCFG=ON,PRICLKEN=OFF,FCMEN=OFF,IESO=OFF,PWRTEN=ON,BOREN=SBORDIS,WDTEN=OFF,MCLRE=EXTMCLR#defineucharunsignedchar#defineuintunsignedint//延时voidDelayMS(uintx){uinti;while(x--){for(i=0;i265;i++);}}voidmain(void){ANSELA=0;TRISA=0;while(1){PORTA=~PORTA;DelayMS(100);}}02从左到右的流水灯/*名称:从左到右的流水灯说明:接在RC口的8个LED从左到右循环依次点亮,产生走马灯效果*/#includep18cxxx.h//#pragmaconfigFOSC=HSMP,PLLCFG=ON,PRICLKEN=OFF,FCMEN=OFF,IESO=OFF,PWRTEN=ON,BOREN=SBORDIS,WDTEN=OFF,MCLRE=EXTMCLR#defineucharunsignedchar#defineuintunsignedint//延时voidDelayMS(uintx){uinti;while(x--){for(i=0;i265;i++);}}voidmain(void){ucharj;ANSELC=0;TRISC=0;while(1){PORTC=0x01;for(j=8;j0;j--){DelayMS(1000);PORTC=PORTC1;}}}038只LED左右来回点亮/*名称:8只LED左右来回点亮说明:程序利用循环移位函数_crol_和_cror_形成来回滚动的效果*/#includep18cxxx.h//#pragmaconfigFOSC=HSMP,PLLCFG=ON,PRICLKEN=OFF,FCMEN=OFF,IESO=OFF,PWRTEN=ON,BOREN=SBORDIS,WDTEN=OFF,MCLRE=EXTMCLR#defineucharunsignedchar#defineuintunsignedint//延时voidDelayMS(uintx){uinti;while(x--){for(i=0;i265;i++);}}voidmain(void){ucharj;ANSELC=0;TRISC=0;PORTC=0x01;while(1){for(j=7;j0;j--){DelayMS(1000);PORTC=PORTC1;}for(j=7;j0;j--){DelayMS(1000);PORTC=PORTC1;}}}06单只数码管循环显示0~9/*名称:单只数码管循环显示0~9说明:主程序中的循环语句反复将0~9的段码送至P0口,使数字0~9循环显示*/#includep18cxxx.h//#pragmaconfigFOSC=HSMP,PLLCFG=ON,PRICLKEN=OFF,FCMEN=OFF,IESO=OFF,PWRTEN=ON,BOREN=SBORDIS,WDTEN=OFF,MCLRE=EXTMCLR#defineucharunsignedchar#defineuintunsignedintconstromucharDSY_CODE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};//延时voidDelayMS(uintx){uinti;while(x--){for(i=0;i265;i++);}}voidmain(void){ucharj;ANSELC=0;TRISC=0;PORTC=0x01;while(1){for(j=0;j10;j++){PORTC=~DSY_CODE[j];DelayMS(1000);}}}078只数码管滚动显示单个数字/*名称:8只数码管滚动显示单个数字说明:数码管从左到右依次滚动显示0~7,程序通过每次仅循环选通一只数码管*/#includep18cxxx.h//#pragmaconfigFOSC=HSMP,PLLCFG=ON,PRICLKEN=OFF,FCMEN=OFF,IESO=OFF,PWRTEN=ON,BOREN=SBORDIS,WDTEN=OFF,MCLRE=EXTMCLR#defineucharunsignedchar#defineuintunsignedintconstromucharDSY_CODE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};//延时voidDelayMS(uintx){uinti;while(x--){for(i=0;i265;i++);}}voidmain(void){uchari;ucharwei=0x80;ANSELC=0;PORTC=0;TRISC=0;ANSELD=0;PORTD=0;TRISD=0;PORTC=0x01;while(1){for(i=0;i8;i++){PORTC=0xff;//关闭显示wei=0x80i;PORTC=~DSY_CODE[i];//发送数字段码PORTD=~wei;//发送位码DelayMS(300);}}}088只数码管动态显示多个不同字符/*名称:8只数码管动态显示多个不同字符说明:数码管动态扫描显示0~7。*/#includep18cxxx.h//#pragmaconfigFOSC=HSMP,PLLCFG=ON,PRICLKEN=OFF,FCMEN=OFF,IESO=OFF,PWRTEN=ON,BOREN=SBORDIS,WDTEN=OFF,MCLRE=EXTMCLR#defineucharunsignedchar#defineuintunsignedintconstromucharDSY_CODE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};//延时voidDelayMS(uintx){uinti;while(x--){for(i=0;i265;i++);}}voidmain(void){uchari;ucharwei=0x80;ANSELC=0;PORTC=0;TRISC=0;ANSELD=0;PORTD=0;TRISD=0;PORTC=0x01;while(1){for(i=0;i8;i++){PORTD=0xff;//关闭显示wei=0x80i;PORTD=~wei;PORTC=~DSY_CODE[i];//发送数字段码DelayMS(2);}}}098只数码管闪烁显示数字串电路如上图/*名称:8只数码管闪烁显示数字串说明:数码管闪烁显示由0~7构成的一串数字本例用动态刷新法显示一串数字,在停止刷新时所有数字显示消失。*/#includep18cxxx.h//#pragmaconfigFOSC=HSMP,PLLCFG=ON,PRICLKEN=OFF,FCMEN=OFF,IESO=OFF,PWRTEN=ON,BOREN=SBORDIS,WDTEN=OFF,MCLRE=EXTMCLR#defineucharunsignedchar#defineuintunsignedintconstromucharDSY_CODE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};//位码表constromucharDSY_IDX[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};//延时voidDelayMS(uintx){uinti;while(x--){for(i=0;i265;i++);}}voidmain(void){uchari,j;ucharwei=0x80;ANSELC=0;PORTC=0;TRISC=0;ANSELD=0;PORTD=0;TRISD=0;PORTC=0x01;while(1){for(i=0;i30;i++){for(j=0;j8;j++){PORTD=0xff;PORTC=~DSY_CODE[j];//发送段码PORTD=~DSY_IDX[j];//发送位码DelayMS(2);}}PORTD=0xff;//关闭所有数码管并延时DelayMS(1000);}}108只数码管滚动显示数字串电路如上图/*名称:8只数码管滚动显示数字串说明:数码管向左滚动显示3个字符构成的数字串*/#includep18cxxx.h//#pragmaconfigFOSC=HSMP,PLLCFG=ON,PRICLKEN=OFF,FCMEN=OFF,IESO=OFF,PWRTEN=ON,BOREN=SBORDIS,WDTEN=OFF,MCLRE=EXTMCLR#defineucharunsignedchar#defineuintunsignedintconstromucharDSY_CODE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};//位码表constromucharNum[]={10,10,10,10,10,10,10,10,2,9,8};//延时voidDelayMS(uintx){uinti;while(x--){for(i=0;i265;i++);}}voidmain(void){uchari,j,k,m;ucharwei=0x80;ANSELC=0;PORTC=0;TRISC=0;ANSELD=0;PORTD=0;TRISD=0;PORTC=0x01;while(1){for(i=0;i15;i++){for(j=0;j8;j++){//发送段码,采用环形取法,从第k个开始取第j个PORTD=0xff;PORTC=~DSY_CODE[Num[(k+j)%11]];m=0x80j;PORTD=~m;//发送位码DelayMS(2);}}k=(k+1)%11;//环形队列首支针k递增,Num下标范围0~10,故对11取余}}11K1-K4控制LED移位/*名称:K1-K4控制LED移位说明:按下K1时,RC口LED上移一位;按下K2时,RC口LED下移一位;按下K3时,RD口LED上移一位;按下K4时,RD口LED下移一位;*/#includep18cxxx.h//#pragmaconfigFOSC=HSMP,PLLCFG=ON,PRICLKEN=OFF,FCMEN=OFF,IESO=OFF,PWRTEN=ON,BOREN=SBORDIS,WDTEN=OFF,MCLRE=EXTMCLR#defineucharunsignedchar#defineuintunsignedintconstromucharDSY_CODE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};//位码表constromucharNum[]={10,10,10,10,10,10,10,10,2,9,8};//延时voidDelayMS(uintx){uinti;while(x--){for(i=0;i265;i++);}}voidMove_LED(void){if((PORTB&0x10)==0){if(PORTC==0x80)PORTC=0x01;elsePORTC=PORTC1;}elseif((PORTB&0x20)==0){if(PORTC==0x01)PORTC=0x80;elsePORTC=

1 / 14
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功