@Mahdi
2017-08-10T07:27:16.000000Z
字数 12338
阅读 1311
code
#include<reg52.h>#define uint unsigned intsbit P2_0 = P2^0;sbit P2_1 = P2^1;void delay(uint z){uint i, j;for(i = z; i > 0; i--)for(j = 114; j>0; j--);}void main(){while(1){P2_0 = 0;delay(10);P2_0 = 1;delay(10);P2_1 = 0;delay(1000);P2_1 = 1;delay(1000);}}//改变占空比 改变电流的强弱
#include<reg52.h>#define uint unsigned intsbit P2_0 = P2^0;void delay(void){uint i;for(i = 1000; i > 0; i--);}void main(){ {P2_0 = 0;delay();P2_0 = 1;delay();}while(1);}
/*2.35Khz*/#include<reg52.h>#define uint unsigned intsbit P2_0 = P2^0;sbit P2_1 = P2^1;void delay(uint z){uint i, j;for(i = z; i > 0; i--)for(j = 50; j>0; j--);}void main(){while(1){P2_0 = 0;delay(1);P2_0 = 1;delay(1);}}
/*双波*/#include<reg52.h>#define uint unsigned intsbit P2_0 = P2^0; //highWavesbit P0_0 = P0^0; //lowWaveuint m;void delay(uint z){uint i, j;for(i = z; i > 0; i--)for(j = 50; j>0; j--);}void highWave(void){P2_0 = 0;delay(1);P2_0 = 1;delay(1);}void lowWave(void){P0_0 = 0;delay(100);P0_0 = 1;delay(1);}void main(){do{lowWave();for(m = 0; m < 10; m++){highWave();}}while(1);}
#include<reg52.h>typedef unsigned char u8;typedef unsigned int u16;sbit beep = P2^0;void delay(u16 i){while(i--);//i = 1大约10um}void main(){ while(1){delay(10000);beep = ~beep;}}
/*改进双波*/#include<reg52.h>#define uint unsigned intsbit P2_0 = P2^0; //highWavesbit P0_0 = P0^0; //lowWaveuint m;void delay(uint z){uint i, j;for(i = z; i > 0; i--)for(j = 50; j>0; j--);}void highWave(void){P2_0 = 0;delay(1);P2_0 = 1;delay(1);}void lowWave(void){P0_0 = 0;delay(100);P0_0 = 1;delay(1);}void main(){do{ P0 = 0x00;P2 = 0x00;lowWave();for(m = 0; m < 10; m++){highWave();}}while(1);}
**
#include<reg52.h>#define uint unsigned intsbit P2_0 = P2^0; //highWavesbit P0_0 = P0^0; //lowWaveuint m;void delay(uint z){uint i, j;for(i = z; i > 0; i--)for(j = 50; j>0; j--);}void highWave(void){P2_0 = 1;delay(1);P2_0 = 0;delay(1);}void lowWave(void){P0_0 = 0;delay(100);P0_0 = 1;delay(1);}void main(){while(1){ P0 = 0x00;P2 = 0x00;lowWave();for(m = 0; m < 10; m++){highWave();}}}
/*条形流水灯*/#include<reg52.h>#define uint unsigned intvoid main(){uint i = 0;uint cnt1 = 0;while(1){P2 = (0xfe<<cnt1);for(i = 0; i < 10000; i++);cnt1++;if(cnt1 >=8){ uint cnt2 = 0;for( ;cnt2 < 8; cnt2++){P2 = (0x7f>>cnt2);for(i = 0; i < 10000; i++);cnt1--;}}}}
移位与数据类型的位数有关,取反也是
##左右移函数实现流水灯#include<reg52.h>#include<intrins.h> //intrinsic 固有的 本征的#define led P2typedef unsigned char u8;typedef unsigned int u16;void delay(u16 i){while(i--);}void main(){u8 i = 0;led = 0xfe;delay(50000);while(1){for(i = 0; i< 7; i++){/*crol是字符循环左移irol是整数循环左移lrol是长整数循环左移<<不循环左移<<左移,最高位移出去了就丢弃,最低位补0._crol_则是循环左移,最高位移出去后不丢弃而是补到最低位,也就是说,最低位不补0.*/led = _crol_(led,1);//_crol_(a,b) a需要左移的值 b左移的位数//0xfe 1111 1110 循环左移后 1111 1101, 1111 1011, 1111 0111delay(50000);}for(i = 0; i < 7; i++){led = _cror_(led,1);delay(50000);}// led = 0;// delay(50000); //450ms// led = 1;// delay(50000);}}
#include<reg52.h>#define uint unsigned intvoid main(){uint m=0,i = 0;char cnt1 = 0;while(1){P2 = ~(~0xfe<<cnt1);for(i = 0; i < 30000; i++);cnt1++;if(cnt1 >=8){ unsigned char cnt2 = 0;for( ;cnt2 < 8; cnt2++){P2 = ~(~0x7f>>cnt2);for(i = 0; i < 3000; i++);cnt1--;}}}}
/*左移函数*/#include <reg52.h>#include <intrins.h>void delay(unsigned int z);void delay(unsigned int z){unsigned int x,y;for(x = z;x > 0;x--)for(y = 114;y > 0;y--);}int main(){int i = 0;unsigned char t;t = 0xfe;P2 = t;while(1){for(i = 0;i < 8;i++){t = _crol_(t,1);P2 = t;delay(500);}}}
#include <reg52.h>sbit A1 = P2^2;sbit A2 = P2^3;sbit A3 = P2^4;unsigned char code LedChar[] = {0xC0, 0xF9, 0xA4, 0xB0,0x99, 0x92, 0x82, 0xF8,0x80, 0x90, 0x88, 0x83,0xC6, 0xA1, 0x86, 0x8E};//将数组放入Flash中存储unsigned char code LedbitA1[] = {0,1};unsigned char code LedbitA2[] = {0,1};unsigned char code LedbitA3[] = {0,1};void main(){ unsigned char cnt = 0; //记录中断次数unsigned char sec = 0; //记录经过的秒数TMOD = 0X01; //设置T0为模式1TH0 = 0XB8; //为T0赋值0xb800TL0 = 0X00;TR0 = 1; //启动T0 溢出后也在运行但需要重载while(1){if(TF0 == 1){TF0 = 0; //T0溢出后软件清0中断标志TH0 = 0xB8; //重载 重新对T0赋初值TL0 = 0x00;cnt++;if(cnt >= 50) //判断T0的溢出次数是否达到50次{cnt = 0;P0 = ~LedChar[sec]; //将当前时间对应在真值表中的值送入P0口A3 = LedbitA3[sec];A2 = LedbitA2[sec];A1 = LedbitA1[sec];sec++;if(sec >= 16)// 当前秒数超过0X0F后,重新计时{sec = 0;}}}}}
/***最大到3.17年***//***138进行位选;245进行段选***/#include<reg52.h>#define uchar unsigned char#define uint unsigned int#define ulong unsigned long intsbit A1 = P2^2;sbit A2 = P2^3;sbit A3 = P2^4;ulong cnt = 0;uint sec = 0;unsigned char code LedChar[] = { //数码管显示字符转换表0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E};unsigned char LedBuff[8] = { //数码管显示缓冲区,共阴极数码管初值 0x00 确保启动时都不亮0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};void init(){TMOD = 0x01;TH0 = 0xDC; //初值越大到达溢出的时间越短,即周期越短,最短时间12/11059200 s = 1/921600 s d=1TL0 = 0x00;TR0 = 1;}void reinit(){TF0 = 0;TH0 = 0xDC; //刷新周期与初值有关 初值为0xB800溢出周期为20msTL0 = 0x00; //刷新周期为(数码管个数*20ms),刷新速度太慢,要在10ms以内} //周期数 = 921600*溢出周期void main(){ uchar i = 0;init();while(1){if(TF0 == 1){reinit();cnt++;if(cnt == 100){cnt = 0;sec++;LedBuff[0] = ~LedChar[sec%10]; //第1位数码管LedBuff[1] = ~LedChar[sec/10%10]; //每点亮一个数码管的间隔时间是定时器的溢出周期LedBuff[2] = ~LedChar[sec/100%10]; //点亮8个数码管至少要在10ms以内 即8*溢出周期<10msLedBuff[3] = ~LedChar[sec/1000%10];LedBuff[4] = ~LedChar[sec/10000%10];LedBuff[5] = ~LedChar[sec/100000%10];LedBuff[6] = ~LedChar[sec/1000000%10];LedBuff[7] = ~LedChar[sec/10000000%10]; //第8位数码管}//以下代码 进行数码管的位选 完成数码管动态扫描刷新if (i == 0){ A3=0; A2=0; A1=0; i++; P0=LedBuff[0]; }else if (i == 1){ A3=0; A2=0; A1=1; i++; P0=LedBuff[1]; }else if (i == 2){ A3=0; A2=1; A1=0; i++; P0=LedBuff[2]; }else if (i == 3){ A3=0; A2=1; A1=1; i++; P0=LedBuff[3]; }else if (i == 4){ A3=1; A2=0; A1=0; i++; P0=LedBuff[4]; }else if (i == 5){ A3=1; A2=0; A1=1; i++; P0=LedBuff[5]; }else if (i == 6){ A3=1; A2=1; A1=0; i++; P0=LedBuff[6]; }else if (i == 7){ A3=1; A2=1; A1=1; i=0; P0=LedBuff[7]; }}}}
#include<reg52.h>#include<intrins.h>#define uint unsigned intuint cnt = 0;sbit wave1 = P0^0;sbit wave2 = P0^3;void delay20ms(void) //误差 0us{unsigned char a,b;for(b=215;b>0;b--)for(a=45;a>0;a--);_nop_(); //if Keil,require use intrins.h_nop_(); //if Keil,require use intrins.h}void main(){EA = 1;TMOD = 0x01;TH0 = 0x3C;TL0 = 0xB0;ET0 = 1;TR0 = 1;while(1){wave1 = 0;delay20ms();wave2 = 1;delay20ms();}}void InterrupteWave2()interrupt 1{TH0 = 0x3C;TL0 = 0xB0;cnt++;if(cnt = 20){wave2 = 0;delay20ms();wave2 = 1;delay20ms();cnt = 0;}}
/*有鬼影版*/#include<reg52.h>#define LCD1602_DB P0//宏定义替换//类型别称定义typedef unsigned char uchar;typedef unsigned int uint;sbit LCD1602_RS = P2^6;sbit LCD1602_RW = P2^5;sbit LCD1602_E = P2^7;//读状态void read_sta(){uchar sta;LCD1602_DB = 0xff;LCD1602_RS = 0;LCD1602_RW = 1;do{LCD1602_E = 1;sta = LCD1602_DB;//输出状态字LCD1602_E = 0;}while(sta & 0x80);}//写指令void write_cmd(uchar cmd){read_sta();LCD1602_RS = 0;LCD1602_RW = 0;LCD1602_DB = cmd;LCD1602_E = 1;LCD1602_E = 0;}//写数据void write_dat(uchar dat){read_sta();LCD1602_RS = 1;LCD1602_RW = 0;LCD1602_DB = dat;LCD1602_E = 1;LCD1602_E = 0;}void main(){/*lcd 初始化*/write_cmd(0x38);//设置16*2显示write_cmd(0x0c);//显示开/关以及光标设置指令write_cmd(0x06);//地址指针加一write_cmd(0x01);//清屏write_cmd(0x80|0x02);//设置显示地址write_dat(0x58);//设置显示内容write_dat(0x75);write_dat(0x65);write_dat(0x20);write_dat(0x53);write_dat(0x6f);write_dat(0x6e);write_dat(0x67);write_dat(0x20);write_dat(0x4a);write_dat(0x75);write_dat(0x6e);write_cmd(0x80|0x42);write_dat(0x48);write_dat(0x65);write_dat(0x6c);write_dat(0x6c);write_dat(0x6f);write_dat(0x20);write_dat(0x57);write_dat(0x6f);write_dat(0x72);write_dat(0x6c);write_dat(0x64);write_dat(0x21);}
#include<lcd1602.h>void ReadSta(){u8 sta;DB = 0xff;RS = 0;RW = 1;do{LCD_E = 1;sta = DB;LCD_E = 0;}while(sta & 0x80);}void WriteCmd(u8 cmd){ReadSta();RS = 0;RW = 0;DB = cmd;LCD_E = 1;LCD_E = 0;}void WriteData(u8 dat){ReadSta();RS = 1;RW = 0;DB = dat;LCD_E = 1;LCD_E = 0;}
#include <reg52.h>sbit fmq=P1^5;void delay(unsigned int i){unsigned char j;while(i--){for(j=0;j<115;j++);}}void yanzou(unsigned char pinlv,unsigned int jiepai){unsigned char pl;unsigned int jp;if(jiepai==1) delay(250);else if(jiepai==2) delay(500);else{for(jp=0;jp<jiepai;jp++){fmq=0;for(pl=0;pl<pinlv;pl++);fmq=1;for(pl=0;pl<pinlv;pl++);}}}void main(){unsigned char i,x;unsigned char code pinlv[]={131,110,98, 87, 73, 87, 110,98, 131,0,110,98, 87, 73, 73, 65, 98, 87,87,87,73,65,73,65,55,58,65,73,65,87,110,98,87,73,110,131,110,98,87,65,73,73,0,87,65,65,73,82,87,98,87,73,131,110,98,0,110,98,87,73,65,55,58,65,73,87,65,65};unsigned int code jiepai[]={110,131,147,494,196,165,131,294,440,1,131,147,165,588,196,440,294,660,330,165,196,880,588,220,262,124,110,196,220,330,131,147,495,196,262,220,131,147,165,220,784,392,2,660,660,220,196,175,330,588,495,196,110,131,147,2,131,147,330,392,440,524,247,220,196,165,880,880};i=68;for(x=0;x<i;x++){yanzou(pinlv[x],jiepai[x]);}}
main.c
/*************************************************************************************** EEPROM-IIC实验 *实现现象:下载程序后数码管后4位显示0,按K1保存显示的数据,按K2读取上次保存的数据,按K3显示数据加一,按K4显示数据清零。最大能写入的数据是255.注意事项:由于P3.2口跟红外线共用,所以做按键实验时为了不让红外线影响实验效果,最好把红外线先取下来。***************************************************************************************/#include "reg52.h" //此文件中定义了单片机的一些特殊功能寄存器#include "i2c.h"#include "lcd.h"typedef unsigned int u16; //对数据类型进行声明定义typedef unsigned char u8;typedef unsigned long int u32;u32 num=0;/******************************************************************************** 函 数 名 : delay* 函数功能 : 延时函数,i=1时,大约延时10us*******************************************************************************/void delay(u16 i){while(i--);}/******************************************************************************** 函数名 :Keypros()* 函数功能 :按键处理函数* 输入 : 无* 输出 : 无*******************************************************************************/void get_from_lux(void){num = Lux_Read(0x00); //读取EEPROM地址1内的数据保存在num中num |= Lux_Read(0x01) << 8; //读取EEPROM地址1内的数据保存在num中num |= Lux_Read(0x02) << 16; //读取EEPROM地址1内的数据保存在num中num |= Lux_Read(0x03) << 24; //读取EEPROM地址1内的数据保存在num中num /= 1000;}//95void itoa(u32 n, char *buf){int i = 0;do {buf[i] = n%10 + '0';n /= 10;i++;} while (n);buf[i] = '\0';}void lux_display(void){int i;char str[16] = {0};get_from_lux();itoa(num, str);for (i = 0; '\0' != str[i]; i++);while (i--) {//LcdWriteCom(0x80 + i );LcdWriteData(str[i]);}}void delay500ms(void) //误差 0us{unsigned char a,b,c;for(c=23;c>0;c--)for(b=152;b>0;b--)for(a=70;a>0;a--);}void main(){LcdInit();while(1){LcdWriteCom(0x80); //设置数据指针起点lux_display( );delay500ms();}}
i2c.c
#include "i2c.h"/******************************************************************************** 函数名 : Delay10us()* 函数功能 : 延时10us* 输入 : 无* 输出 : 无*******************************************************************************/void Delay10us(){unsigned char a,b;for(b=1;b>0;b--)for(a=2;a>0;a--);}/******************************************************************************** 函数名 : I2cStart()* 函数功能 : 起始信号:在SCL时钟信号在高电平期间SDA信号产生一个下降沿* 输入 : 无* 输出 : 无* 备注 : 起始之后SDA和SCL都为0*******************************************************************************/void I2cStart(){SDA=1;Delay10us();SCL=1;Delay10us();//建立时间是SDA保持时间>4.7usSDA=0;Delay10us();//保持时间是>4usSCL=0;Delay10us();}/******************************************************************************** 函数名 : I2cStop()* 函数功能 : 终止信号:在SCL时钟信号高电平期间SDA信号产生一个上升沿* 输入 : 无* 输出 : 无* 备注 : 结束之后保持SDA和SCL都为1;表示总线空闲*******************************************************************************/void I2cStop(){SDA=0;Delay10us();SCL=1;Delay10us();//建立时间大于4.7usSDA=1;Delay10us();}/******************************************************************************** 函数名 : I2cSendByte(unsigned char dat)* 函数功能 : 通过I2C发送一个字节。在SCL时钟信号高电平期间,保持发送信号SDA保持稳定* 输入 : num* 输出 : 0或1。发送成功返回1,发送失败返回0* 备注 : 发送完一个字节SCL=0,SDA=1*******************************************************************************/unsigned char I2cSendByte(unsigned char dat){unsigned char a=0,b=0;//最大255,一个机器周期为1us,最大延时255us。for(a=0;a<8;a++)//要发送8位,从最高位开始{SDA=dat>>7; //起始信号之后SCL=0,所以可以直接改变SDA信号dat=dat<<1;Delay10us();SCL=1;Delay10us();//建立时间>4.7usSCL=0;Delay10us();//时间大于4us}SDA=1;Delay10us();SCL=1;while(SDA)//等待应答,也就是等待从设备把SDA拉低{b++;if(b>200) //如果超过2000us没有应答发送失败,或者为非应答,表示接收结束{SCL=0;Delay10us();return 0;}}SCL=0;Delay10us();return 1;}/******************************************************************************** 函数名 : I2cReadByte()* 函数功能 : 使用I2c读取一个字节* 输入 : 无* 输出 : dat* 备注 : 接收完一个字节SCL=0,SDA=1.*******************************************************************************/unsigned char I2cReadByte(){unsigned char a=0,dat=0;SDA=1; //起始和发送一个字节之后SCL都是0Delay10us();for(a=0;a<8;a++)//接收8个字节{SCL=1;Delay10us();dat<<=1;dat|=SDA;Delay10us();SCL=0;Delay10us();}return dat;}/******************************************************************************** 函数名 : void Lux_Write(unsigned char addr,unsigned char dat)* 函数功能 : 往24c02的一个地址写入一个数据* 输入 : 无* 输出 : 无*******************************************************************************/void Lux_Write(unsigned char addr,unsigned char dat){I2cStart();I2cSendByte(0xa0);//发送写器件地址I2cSendByte(addr);//发送要写入内存地址I2cSendByte(dat); //发送数据I2cStop();}/******************************************************************************** 函数名 : unsigned char Lux_Read(unsigned char addr)* 函数功能 : 读取24c02的一个地址的一个数据* 输入 : 无* 输出 : 无*******************************************************************************/unsigned char Lux_Read(unsigned char addr){unsigned char num;I2cStart();I2cSendByte(0x94); //发送写器件地址I2cSendByte(addr); //发送要读取的地址I2cStart();I2cSendByte(0x95); //发送读器件地址num=I2cReadByte(); //读取数据I2cStop();return num;}