VegoSong

记录点滴生活,学习心得,分享经验

寒假电协单片机培训代码记录

/*
8个灯闪烁程序
*/
#include 
#include 
unsigned int i;
void main()
{
while(1)
{
P0 = 0x00; //8个灯全部亮
for(i=0;i<30000;i++) ;//延时
P0 = 0xff; //8个灯全熄
for(i=0;i<30000;i++) ;//延时
}

}
-------------------------------------------------------------
/*
跑马灯效果程序
*/
#include 
#include 
unsigned char LED;
void delay(unsigned int i);
unsigned int i;
void main()
{
while(1) //*******此部分用自带库函数实现8个LED灯向右的跑马效果
{
unsigned char LED;
LED = 0xfe;

while(1)
{
P0 = LED;
delay(10);
LED = _cror_(LED,1); //循环左移1位,点亮下一个LED 此函数位库函数
}
break;
}
}
//*******自定义延时子函数************//
void delay(unsigned int i)
{
unsigned int j;
for(i; i > 0; i--) //循环i*j次 机器在这里执行需要一段时间 也就达到了延时效果
for(j = 1246; j > 0; j--);
}
-------------------------------------------------------------------------------
/*
流水灯效果程序
*/
#include
void delay(unsigned int i);
void main()
{
while(1)
{
unsigned char LED;
LED = 0xff;
while(1)
{
P0 = LED ;
delay(50);
LED =LED<<1;
if(LED ==0X00 )
{
P0 = LED;
delay(50);
LED = 0xff;
}
}
}
}
void delay(unsigned int i)
{
unsigned int j;
for(i; i > 0; i--)
for(j = 1246; j > 0; j--);
}
-----------------------------------------------------------------

/********************************************************
任务:用定时器 T1 ,方式1 ,实现50ms定时中断,
并利用这个“10ms”实现秒计数,用数码管显示
********************************************************/
#include 
//此表为8个数码管位选控制, 共阴数码管 1-8个
unsigned char Disp_Bit[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdF,0xbF,0x7F}; //位选控制 查表的方法控制
//此表为 LED 的字模, 共阴数码管 "0-9" " - " 0100 0000
unsigned char Disp_Code[11] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40}; //段码控制
unsigned int Second;
unsigned char Int_Count; // 255 +1 = 0
void delay(unsigned int i);
void Timer_Ini(void);
void main()
{
unsigned char temp[4];
unsigned char i;
Timer_Ini();
while(1)
{
if(Int_Count>=20) //
{
Second++; // 1s +1
Int_Count = 0;
}
temp[0] = Second%10000/1000; // 千位 1s = 10ms * 100
temp[1] = Second%1000/100; // 百位
temp[2] = Second%100/10; // 十位
temp[3] = Second%10; // 个位
for(i=0;i<4;i++)
{
P1 = Disp_Bit[i]; //第i个数码管位选 1011 1111 0xbf
P0 = Disp_Code[temp[i]]; //第i个数码管的编码
delay(2);
}
}
}
void miaobiao(void) interrupt 3
{
TH1 = 0x3C; // (65536 - X)*1us = 50000us x = 15536
TL1 = 0xB0;

Int_Count++; // 50ms +1
}
//*******自定义延时子函数************//
void delay(unsigned int i)
{
unsigned int j;
for(i; i > 0; i--) //循环i*j次 机器在这里执行需要一段时间 也就达到了延时效果
for(j = 150; j > 0; j--);
}
void Timer_Ini(void)
{
TMOD = 0x10;
TH1 = 0x3C; // (65536 - X)*1us = 50000us x = 15536
TL1 = 0xB0;
EA = 1;
ET1 = 1;
TR1 = 1;
}

------------------------------------------------------------------
//--简单独立按键控制LED实验--//
//--包含你要使用的头文件--//
#include //此文件中定义了51的一些特殊功能寄存器
#include 
//--定义要使用的IO口--//
#define GPIO_KEY P1 //独立键盘用P1口
#define GPIO_LED P0 //led使用P0口
//--声明全局函数--//
void Delay10ms(unsigned int c); //延时10ms
unsigned char Key_Scan();
/*******************************************************************************
* 函 数 名 : main
* 函数功能 : 主函数
* 输 入 : 无
* 输 出 : 无
*******************************************************************************/
void main(void)
{
while (1)
{
GPIO_LED = ~GPIO_KEY;//点亮LED灯
}
}
------------------------------------------------------------------------
/****************************************************

任务:设置外部中断INT0,采用下降沿触发方式工作 P3.2
并用一个变量记录中断次数,数码管显示出来

作业:首先,重新实现一遍 目前的功能
然后,改代码,实现设置外部中断0,采用低电平触发方式工作,
设置外部中断1,采用下降沿触发方式工作,
用两个变量分别记录它们 的中断次数,并分别用四位数码管显示出来

****************************************************/
#include

//此表为 LED 的字模, 共阴数码管"0-9" " - "
unsigned char code Disp_Code[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40}; //段码控制

//此表为8个数码管位选控制, 共阴数码管 1-8个
unsigned char code Disp_Bit[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdF,0xbF,0x7F}; //位选控制 查表的方法控制

void delay(unsigned int i)
{
char j;
for(i; i > 0; i--)
for(j = 200; j > 0; j--);
}

unsigned int Interrupt_CNT;

bit Int_Flag;

void main()
{
unsigned char temp[4];
unsigned char temp_code[4];
unsigned char i;
unsigned int Cycle_Count;

IT0 = 1;
EA = 1;
EX0 = 1;

while(1)
{
temp[0] = Interrupt_CNT%10000/1000;
temp[1] = Interrupt_CNT%1000/100;
temp[2] = Interrupt_CNT%100/10;
temp[3] = Interrupt_CNT%10;

for(i=0;i<4;i++)
{
temp_code[i] = Disp_Code[temp[i]];
}

for(i=0;i<4;i++)
{
P1 = Disp_Bit[i]; //位选
P0 = temp_code[i]; //
delay(10);
P0 = 0x00; //消隐
}

if(Int_Flag==1)
{
Cycle_Count++;
if(Cycle_Count>5000)
{
EX0 = 1;
Int_Flag = 0;
Cycle_Count = 0;

}
}
}
}

void ZHONGDUAN(void) interrupt 0
{
EX0 = 0; //关中断
Interrupt_CNT++;

Int_Flag = 1;

//EX0 = 1;

}
点赞

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注