單片機C語言程序舉例(二)
#include
#include
#include
unsigned int hour,minute,second,count;
sbit RS = P2^4;//Pin4
sbit RW = P2^5; //Pin5
sbit E
sbit BLCtrl = P2^7;
sbit KEY = P3^2;
#define Data
char data TimeNum[]="
char data Test1[]="
void DelayUs(unsigned char us)//delay us
{
}
void DelayMs(unsigned char ms)//delay Ms
{
}
void WriteCommand(unsigned char c)
{
}
void WriteData(unsigned char c)
{
}
void ShowChar(unsigned char pos,unsigned char c)
{
}
void ShowString (unsigned char line,char *ptr)
{
}
void InitLcd()
{
}
/////////////////////////////////////////////////////////////////
void main(void)
{
InitLcd();//初始化液晶
DelayMs(15);
sprintf(Test1," www.haotun.com ");//第一行
ShowString(0,Test1);
sprintf(TimeNum,"LCM1602 Test ok!");//第二行
ShowString(1,TimeNum);
while(1){
if(!KEY)
}
}
9.步進電機控制
#include
unsigned char code F_Rotation[4]={0x02,0x04,0x08,0x10};//正轉表格
unsigned char code B_Rotation[4]={0x10,0x08,0x040,0x02};//反轉表格
void Delay(unsigned int i)//延時
{
}
main()
{
}
10. 直流電機調速
#include
sbit KEY1 = P3^1;
sbit PWM = P1^5;
unsigned char CYCLE; //定義周期 該數字X基準定時時間 如果是10 則周期是10 x 0.1ms
unsigned char PWM_ON ;//定義高電平時間
void delay(unsigned int cnt)
{
}
main()
{
unsigned char PWM_Num;//定義檔位
TMOD |=0x01;//定時器設置 1ms in 12M crystal
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;//定時1mS
IE= 0x82;
TR0=1;
CYCLE = 10;// 時間可以調整 這個是10步調整 周期10ms 8位PWM就是256步
while(1)
if(!KEY1)
}
void tim(void) interrupt 1 using 1
{
static unsigned char count; //
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;//定時1mS
if (count==PWM_ON)
if(count == CYCLE)
}
11.秒表
#include
code unsigned char tab[]=
unsigned char Dis_Shiwei;//定義十位
unsigned char Dis_Gewei; //定義個位
unsigned char S_Dis_Shiwei;//定義秒十位
unsigned char S_Dis_Gewei; //定義秒個位
unsigned char second,msecond;
void CLR(void);
void delay(unsigned int cnt)
{
}
main()
{
EX0=1;//外部中斷0設置
IT0=1;
EX1=1;//外部中斷1設置
IT1=1;
TMOD |=0x01;//定時器設置 10ms in 12M crystal
TH0=0xd8;
TL0=0xf0;
ET0=1;
TR0=0;
EA=1;
CLR();
while(1)
}
void tim(void) interrupt 1 using 1
{
TH0=0xd8;//重新賦值
TL0=0xf0;
msecond++;
if (msecond==100)
}
void ISR_INT0(void) interrupt 0 using 1
{
}
void ISR_INT1(void) interrupt 2 using 1
{
if(TR0==0)//停止時才可以清零
}
void CLR(void)
{
}
12.花樣燈
#include
unsigned char code seg[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0xff,0xff,0x00,0,0x55,0x55,0xaa,0xaa};//rom允許情況可以無限添加
////////////////////////////////////////////////////////
void delay(unsigned int cnt)//簡易延時
{
}
///////////////////////////////////////////////////////
main()
{
////////////////////////////////////
}
13.紅綠燈
#include
bit red,green,yellow,turnred;
code unsigned char tab[]=
unsigned char Dis_Shiwei;//定義十位
unsigned char Dis_Gewei; //定義個位
void delay(unsigned int cnt)
{
}
main()
{
TMOD |=0x01;//定時器設置 10ms in 12M crystal
TH0=0xd8;
TL0=0xf0;
IE= 0x82;
TR0=1;
P1=0xfc;//紅燈亮
red =1;
while(1)
}
void tim(void) interrupt 1 using 1
{
static unsigned char second=60,count; //初值99
TH0=0xd8;//重新賦值
TL0=0xf0;
count++;
if (count==100)
}
評論