《单片机C的语言程序设计方案》由会员分享,可在线阅读,更多相关《单片机C的语言程序设计方案(75页珍藏版)》请在金锄头文库上搜索。
1、1单片机 C 的语言程序设计方案第 01 篇 基础程序设计01 闪烁的 LED/* 名称:闪烁的 LED说明:LED 按设定的时间间隔闪烁*/#include#define uchar unsigned char#define uint unsigned intsbit LED=P10;/延时void DelayMS(uint x)uchar i;while(x-)for(i=0;i#include#define uchar unsigned char#define uint unsigned int/延时2void DelayMS(http:/ x)uchar i;while(x-)for(
2、i=0;i#include#define uchar unsigned char#define uint unsigned int/延时void DelayMS(uint x)uchar i;while(x-)for(i=0;i#define uchar unsigned char#define uint unsigned intuchar code Pattern_P0=0xfc,0xf9,0xf3,0xe7,0xcf,0x9f,0x3f,0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xe7,0xdb,0xbd,0x7e,0xbd,0xdb,0x
3、e7,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,0x
4、ff,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;uchar code Pattern_P2=0xff,0xff,
5、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,
6、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,
7、0xff,0xff,0xff,0xff,0xff,0xff,0x00,0xff,0x00,0xff,0x00,0xff,0x00,0xff;/延时4void DelayMS(uint x)uchar i;while(x-)for(i=0;i# http:/ uint unsigned intsbit RED_A=P00; /东西向灯sbit YELLOW_A=P01;sbit GREEN_A=P02;sbit RED_B=P03; /南北向灯sbit YELLOW_B=P04;sbit GREEN_B=P05;uchar Flash_Count=0,Operation_Type=1; /闪烁次
8、数,操作类型变量/延时void DelayMS(uint x)uchar i;5while(x-) for(i=0;i6#include#define uchar unsigned char#define uint unsigned intuchar code DSY_CODE=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff;/延时void DelayMS(uint x)uchar t;while(x-) for(t=0;t#include#define uchar unsigned char#define uint unsigne
9、d intuchar code DSY_CODE=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90;/延时void DelayMS(uint x)uchar t;while(x-) for(t=0;t#include#define uchar unsigned char#define uint unsigned intuchar code DSY_CODE=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90;/延时void DelayMS(uint x)uchar t;while(x-) for(
10、t=0;t#define uchar unsigned char#define uint unsigned int/段码表uchar code DSY_CODE=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90;/位码表uchar code DSY_IDX=0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80;/延时void DelayMS(uint x)uchar t;while(x-) for(t=0;t#include#define uchar unsigned char#define uint unsigned
11、 int/段码表uchar code DSY_CODE=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff;/下面数组看作环形队列,显示从某个数开始的 8 个数( 10 表示黑屏)uchar Num=10,10,10,10,10,10,10,10,2,9,8;/延时void DelayMS(uint x)uchar t;while(x-) for(t=0;t#include#define uchar unsigned char#define uint unsigned int/延时void DelayMS(uint x)uchar i;
12、while(x-) for(i=0;i#define uchar unsigned char#define uint unsigned intsbit LED1=P00;sbit LED2=P01;sbit LED3=P02;sbit LED4=P03;sbit K1=P10;sbit K2=P11;sbit K3=P12;sbit K4=P13;/延时void DelayMS(uint x)uchar i;while(x-) for(i=0;i#define uchar unsigned char#define uint unsigned int/延时void DelayMS(uint x)
13、uchar i;while(x-) for(i=0;i4;k=0;/检查 1 所在位置,累加获取按键号 kwhile(Key_State!=0)k+;Key_State=1;/根据按键号 k 进行 4 种处理switch(k)case 1: if(P0=0x00) P0=0xff;P0#define uchar unsigned char#define uint unsigned int/段码uchar code DSY_CODE=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff;/位码uchar code DSY_Index=0x8
14、0,0x40,0x20,0x10,0x08,0x04,0x02,0x01;/待显示到各数码管的数字缓冲(开始仅在 0 位显示 0,其他黑屏)uchar Display_Buffer=0,10,10,10,10,10,10,10;/延时void DelayMS(uint x)uchar i;while(x-) for(i=0;i8) Key_Counts=8;Display_BufferKey_Counts-1=Key_Counts;break;case 0xfd: if(Key_Counts0)Display_Buffer-Key_Counts=10;break;case 0xfb: Disp
15、lay_Buffer0=0;for(i=1;i#include#define uchar unsigned char#define uint unsigned int/段码uchar code DSY_CODE=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff;/待显示的 3 位缓冲uchar Num_Buffer=0,0,0;/按键代码,按键计数uchar Key_Code,Key_Counts=0;/延时void DelayMS(uint x)15uchar i;while(x-) for(i=0;i0) Key_Counts-;break;case 0xfb: Key_Counts=0;Key_Code=0xff;1616 4X4 矩阵键盘控制条形 LED 显示/* 名称:4X4 矩阵键盘控制条形 LED 显示说明:运行本例时,按下的按键值越大点亮的LED 越多。*/#include#include#define uchar unsigned char#define uint unsigned int/矩阵键盘按键特征码表uchar code KeyCodeTable=0x11,0x12,0x14,0x18,0x21,0