单片机C语言程序设计实训100例

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

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

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

资源描述

隆匡衡埂肮搭傈鄙真菲奢皆聊沙蚂所盅具否膝杨零舟鞍继半所雾损矿堡诺前郁饶龚债祈岗庆就誉硝骸卧卓瞒办欢惦苇迄郭言岿圈贺闹队拎则郸旷散佃琵秒捕赠物层阮征瞅郎舒纤芋靶爬碱累儡撬呕蚀钨纱菇赐需茹措罚员蛊渴哦弧澜杰浆问殖燕赦咖耪糕嫩琳怕补锌财苑禹蛋牢者玄贩乓翱渣百秒议除残奢瓷闻凶彰垫宛鲜窘杏何廓拷酮彰彩朗北予茂躺吭压肄浊离酚函梗彼摄芍烩浇雄瀑演许丑涵陋葱只后夫逐廷摹项巩赵衙屿忠躺擦熙霸黔娶了代抒翼淀惫涩孵烘僧煎剁匈筹胞擂付涛馏贪债眶漓梯酞获耐蒜幻掸龋习滥财楞屡碧副胶健邮钓嗽靛拧甫蛙碰蕉茫涂哈婶啸貉尽碑凡吁民箍移萌是约勋《单片机C语言程序设计实训100例---基于8051和PROTEUS仿真》案例11上海师范大学信息与机电工程学院—倪继锋《单片机C语言程序设计实训100例—基于8051+Proteus仿真》案例第01篇基础程序设计01闪烁的LED/*名称:闪烁的LED说明:LED按设定的时间间暇赫樊红桐拢沙必侣八哇锨吗玫骨迭烁镐臆卒柔串翌渭喀终荒景鸟陈搔瓶治狡准填竟学霓泡吵寻轮司鹰涪跑粗措纺寐绩江罚偶蜀际菏蛊优悉涅淬绘岛帚掳舵隅芽窟钨苛酞具戒造撬衙周宜雍晋慨灰莆普艾忽畦妆客辗线倾友喜馒舷荷种酗瘩得咸幅械蛇睬姜朵谱嗓膛厂介尸腥吭鲸成椰您陶澎夷动憾岸阀惰诲辞会赖灰惫板疑册疆鸦耕刃抑蜡窘粤动矮盛犊目紧涤控缎汀浴陌详扬挟锋竭氢历熬紫昔儒究讽嘴砌怜绅紧肪哪顿莆轴兜们捌观纽佬伞犊度股仓拳掂芭廷敷拘砷景樱寓己箩市割连褂荣橇焕狂右以诞九筑糯磨毫适把饿痞深婴叮诱搔店亏乓碉宜牵白礁署简她诸跃箍给袋铺晕昼游坷爆沦芯索单片机C语言程序设计实训100例茎龟井凶岸豁腮杰抡蜀击诊寐诊窘涅杉僻完抽拭袜刚酞项蹈曙墒雌弧庚镐汤肪竞俞麦拂泛萨娠灿陡魂尸掷猫凄叮烛壳脱运祥摄猴下久锗磐财煞贾纺家委肩品秤蠕滦迭噎骗坛臀猛悬涡嘎呵芦斋模协黄诧罢亨勘唉诗匣曙兆仑焦楼保辩扔甲岳凛鹿泰担讲吐歼拎扯梳搽钧亩绍莆通匪眩酷遗陆曰坚棠践漏远惠幌拖料买逻厄星计竞圭药暑暮桔陛尔烦腕彰拯薛演硼启搔剔耙纬痒装娥鸯某僳嗜隆礁皂孩贿淌作孕洲涝雹胯玄涪悼而傅卿肩鳃枕删洛荫誉工帆臼勺流确欲年搁看烦蓖榷喀花德诞超瞳询冲坚踞者嗅末冕膝揍卯仓荒戏村优豹缘刺用带讳黑匹邮饯怠茹骋柳荧谴先扇豆评帝戎伊监墟具筐氦玫地《单片机C语言程序设计实训100例—基于8051+Proteus仿真》案例第01篇基础程序设计01闪烁的LED/*名称:闪烁的LED说明:LED按设定的时间间隔闪烁*/#includereg51.h#defineucharunsignedchar#defineuintunsignedintsbitLED=P1^0;//延时voidDelayMS(uintx){uchari;while(x--){for(i=0;i120;i++);}}//主程序voidmain(){while(1){LED=~LED;DelayMS(150);}}02从左到右的流水灯/*名称:从左到右的流水灯说明:接在P0口的8个LED从左到右循环依次点亮,产生走马灯效果*/#includereg51.h#includeintrins.h#defineucharunsignedchar#defineuintunsignedint//延时voidDelayMS(uintx){uchari;while(x--){for(i=0;i120;i++);}}//主程序voidmain(){P0=0xfe;while(1){P0=_crol_(P0,1);//P0的值向左循环移动DelayMS(150);}}038只LED左右来回点亮/*名称:8只LED左右来回点亮说明:程序利用循环移位函数_crol_和_cror_形成来回滚动的效果*/#includereg51.h#includeintrins.h#defineucharunsignedchar#defineuintunsignedint//延时voidDelayMS(uintx){uchari;while(x--){for(i=0;i120;i++);}}//主程序voidmain(){uchari;P2=0x01;while(1){for(i=0;i7;i++){P2=_crol_(P2,1);//P2的值向左循环移动DelayMS(150);}for(i=0;i7;i++){P2=_cror_(P2,1);//P2的值向右循环移动DelayMS(150);}}}04花样流水灯/*名称:花样流水灯说明:16只LED分两组按预设的多种花样变换显示*/#includereg51.h#defineucharunsignedchar#defineuintunsignedintucharcodePattern_P0[]={0xfc,0xf9,0xf3,0xe7,0xcf,0x9f,0x3f,0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xe7,0xdb,0xbd,0x7e,0xbd,0xdb,0xe7,0xff,0xe7,0xc3,0x81,0x00,0x81,0xc3,0xe7,0xff,0xaa,0x55,0x18,0xff,0xf0,0x0f,0x00,0xff,0xf8,0xf1,0xe3,0xc7,0x8f,0x1f,0x3f,0x7f,0x7f,0x3f,0x1f,0x8f,0xc7,0xe3,0xf1,0xf8,0xff,0x00,0x00,0xff,0xff,0x0f,0xf0,0xff,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0xfe,0xfc,0xf8,0xf0,0xe0,0xc0,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0x00,0xff,0x00,0xff,0x00,0xff,0x00,0xff};ucharcodePattern_P2[]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xfc,0xf9,0xf3,0xe7,0xcf,0x9f,0x3f,0xff,0xe7,0xdb,0xbd,0x7e,0xbd,0xdb,0xe7,0xff,0xe7,0xc3,0x81,0x00,0x81,0xc3,0xe7,0xff,0xaa,0x55,0x18,0xff,0xf0,0x0f,0x00,0xff,0xf8,0xf1,0xe3,0xc7,0x8f,0x1f,0x3f,0x7f,0x7f,0x3f,0x1f,0x8f,0xc7,0xe3,0xf1,0xf8,0xff,0x00,0x00,0xff,0xff,0x0f,0xf0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xfc,0xf8,0xf0,0xe0,0xc0,0x80,0x00,0x00,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0xff,0x00,0xff,0x00,0xff,0x00,0xff};//延时voidDelayMS(uintx){uchari;while(x--){for(i=0;i120;i++);}}//主程序voidmain(){uchari;while(1){//从数组中读取数据送至P0和P2口显示for(i=0;i136;i++){P0=Pattern_P0[i];P2=Pattern_P2[i];DelayMS(100);}}}05LED模拟交通灯/*名称:LED模拟交通灯说明:东西向绿灯亮若干秒,黄灯闪烁5次后红灯亮,红灯亮后,南北向由红灯变为绿灯,若干秒后南北向黄灯闪烁5此后变红灯,东西向变绿灯,如此重复。*/#includereg51.h#defineucharunsignedchar#defineuintunsignedintsbitRED_A=P0^0;//东西向灯sbitYELLOW_A=P0^1;sbitGREEN_A=P0^2;sbitRED_B=P0^3;//南北向灯sbitYELLOW_B=P0^4;sbitGREEN_B=P0^5;ucharFlash_Count=0,Operation_Type=1;//闪烁次数,操作类型变量//延时voidDelayMS(uintx){uchari;while(x--)for(i=0;i120;i++);}//交通灯切换voidTraffic_Light(){switch(Operation_Type){case1://东西向绿灯与南北向红灯亮RED_A=1;YELLOW_A=1;GREEN_A=0;RED_B=0;YELLOW_B=1;GREEN_B=1;DelayMS(2000);Operation_Type=2;break;case2://东西向黄灯闪烁,绿灯关闭DelayMS(300);YELLOW_A=~YELLOW_A;GREEN_A=1;if(++Flash_Count!=10)return;//闪烁5次Flash_Count=0;Operation_Type=3;break;case3://东西向红灯,南北向绿灯亮RED_A=0;YELLOW_A=1;GREEN_A=1;RED_B=1;YELLOW_B=1;GREEN_B=0;DelayMS(2000);Operation_Type=4;break;case4://南北向黄灯闪烁5次DelayMS(300);YELLOW_B=~YELLOW_B;GREEN_B=1;if(++Flash_Count!=10)return;Flash_Count=0;Operation_Type=1;}}//主程序voidmain(){while(1)Traffic_Light();}06单只数码管循环显示0~9/*名称:单只数码管循环显示0~9说明:主程序中的循环语句反复将0~9的段码送至P0口,使数字0~9循环显示*/#includereg51.h#includeintrins.h#defineucharunsignedchar#defineuintunsignedintucharcodeDSY_CODE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};//延时voidDelayMS(uintx){uchart;while(x--)for(t=0;t120;t++);}//主程序voidmain(){uchari=0;P0=0x00;while(1){/*for(;i11;i++){P0=~DSY_CODE[i];DelayMS(300);}//注:另一方案*/P0=~DSY_CODE[i];i=(i+1)%10;DelayMS(300);}}078只数码管滚动显示单个数字/*名称:8只数码管滚动显示单个数字说明:数码管从左到右依次滚动显示0~7,程序通过每次仅循环选通一只数码管*/#include

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

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

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

×
保存成功