1、 头文件
因为习惯原因和节约时间,虽然15单片机有专门的头文件,但是内部框架和51单片机是同样的,所以可以直接使用51的头文件,因为15的单片有多几个IO口,所以需要多定义几个IO口,另外引入头文件还有一些其他的,通过#include方式引入 ,代码模板如下。
- #include "reg52.h" //包含51单片机寄存器定义的头文件
- #include "intrins.h" //包含_nop_()定义的头文件
- #include "iic.h" //通信驱动
- #include "AT24C02.h" //数据写入
- #include "display.h" //数码管
- #include "cls.h" //关闭外设
- #include "delay.h" //延迟
- #include "key.h" //按键
- #include "interrupt.h"//中断
- sfr P4 = 0xC0; //定义P4 io口
- sbit P42 = P4^2;
- sbit P44 = P4^4;
2、 按键函数
(1) 普通按键
- sbit S7 = P3^0; //给io口命名
- unsigned char count=0;
- unsigned char key_value = 0;
- void key_scanf()
- {
- if(S7==0) //判断按键按下
- {
- Delay(5); //消抖 需要引入延时函数
- if(S7==0) //再次判断按键按下
- {
- key_value=1; //判断按下的数值
- count++; //判断按键按下次数
- while(!S7);
- }
- }
- }
(2) 矩阵按键
需要提前定义 n变量和key_value,count变量
- void key_scan()
- {
- P3=0xf0;P44=1;P42=1;
- if(P3!=0xf0||P44!=1||P42!=1)
- {
- Delay50ms();
- if(P3!=0xf0||P44!=1||P42!=1)
- {
- P3=0xf0;P44=1;P42=1;
- if(P44==0) n=0; //判断哪一列被拉低
- else if(P42==0) n=1;
- else if((P3&0X10)==0) n=3;
- else if((P3&0x20)==0) n=2;
- P3=0x0F;P44=0;P42=0; //行信号置零,列信号置一
- if((P3&0x01)==0) key_value =n; //判断哪一行被拉低
- else if((P3&0x02)==0) key_value =n+4;
- else if((P3&0x04)==0) key_value =n+8;
- else if((P3&0x08)==0) key_value =n+12;
- count++; //判断按键按下操作
- while(P3!=0x0F);
- }
- }
- }
3、 延时函数
延时函数可以通过软件直接生成精准延时,然后对其进行修改,这里的是一毫秒。通过形式参数可以实现调用不同时间的延时,因为延时会卡住单片机无法继续线下运行,因此中断中尽量少用。_nop_()需要引入同文件#include“intrins.h”。
- void Delay(unsigned int t) //@11.0592MHz
- {
- unsigned char i, j;
- for(;t>0;t--)
- {
- _nop_();
- _nop_();
- _nop_();
- i = 11;
- j = 190;
- do
- {
- while (--j);
- } while (--i);
- }
- }
4、 关闭外设
因为单片机初始化会默认所有引脚高电平,容易对外设产生影响,因此需要上电过乘进行外设的关闭。
- void cls_buzz(void) //关闭蜂鸣器,继电器等。
- {
- P2=(P2&0X1F)|0XA0;
- P0=0x00;
- P2&=0X1F;
- }
- void cls_led(void) //关闭led灯
- {
- P2=(P2&0X1F)|0X80;
- P0=0XFF;
- P2 &= 0X1F;
- }
- void cls_display(void) //关闭数码管
- {
- P2 = (P2&0x1F|0xC0);
- P0 = 0xff; //关闭位选起消影作用
- P2 &= 0x1f;
- }
5、 流水灯
流水灯千变万化,为了适用各种流水灯的操作,这里编写了能过适用各种方式的流水灯。可以静态点灯调用也可以动态点灯调用。
- unsigned char led_flash[8]={0x80,0x40,0x20,0x10,0x08,0x40,0x02,0x01}//通过数组方法点灯
- unsigned char led_num = 0;
- void led_location(unsigned char Led_Mode,unsigned char Led_Num)
- {
- P0=0xff; //消隐
- P2=(P2&0x1f)|0x80;
- switch(Led_Mode)
- {
- case 0:P0=0xff;break;
- case 1:P0=~(0x01<<Led_Num);break;
- case 2:P0=~(0x80>>Led_Num);break;
- case 3:P0=led_flash[Led_Num];break;
- case 4:P0=0x00;break;
- case 5:P0=~0x80;break;
- }
- P2&=0x1f;
- }
- void led_flash_mode1() //模式1的方法闪烁流水灯将函数放入循环中
- {
- if(led_flash_mode1_switch == 0xff)//led流水灯运行标志位
- {
- delay(500);//如果在中断中可以删除延迟,调节中断间隔。
- if(++led_num<8)
- {
- void led_location(1,led_num);
- }else
- {
- led_num=0;
- }
- }
- }
6、按键功能处理
需要放在循环中
- void key_pro()
- {
- if(count!=0)
- {
- count=0;
- switch(key_value)
- {
- case 1:led_flash_mode1_switch =~led_flash_mode1_switch;break;
- case 2:break;
- case 3:break;
- }
- }
- }
7、 数码管动态显示
动态显示就可以当作静态的功能,因此这里只用动态的方法。调用只需要对数组
- code unsigned char tab[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0X7f,0XFF}; //数码管显示数字表
- // 0 1 2 3 4 5 6 7 8 9 . 无
- // 0 1 2 3 4 5 6 7 8 9 10 11
- unsigned char dspbuf[8] = {11,11,11,11,11,11,11,11};
- unsigned char dspcom = 0;
- void display(void) //数码管需要放入中断中扫描
- {
- P0 = 0xff;
- P2 = (P2&0x1F|0xE0);
- P2 &= 0x1f;
- P2 = (P2&0x1F|0xC0);
- P0 = (1<<dspcom);
- P2 = 0x1f;
- P2 = (P2&0x1F|0xE0);
- P0 = tab[dspbuf[dspcom]];
- P2 = 0x1f;
- if(++dspcom == 8)
- dspcom = 0; //如
- }
8、 中断函数
可以通过软件直接生成。
- void Timer0Init(void) //1毫秒@11.0592MHz
- {
- AUXR |= 0x80; //定时器时钟1T模式
- TMOD &= 0xF0; //设置定时器模式
- TL0 = 0xCD; //设置定时初值
- TH0 = 0xD4; //设置定时初值
- TF0 = 0; //清除TF0标志
- TR0 = 1; //定时器0开始计时
- ET0 = 1;
- EA = 1;
- }
- void T0time() interrupt 1
- {
- display();
- }
9、 温度函数
温度函数为调用官方提供的onewire.h因为15芯片的时间要快12倍因此需要将延迟函数进行变慢12倍。然后加入读取温度函数。
- void Delay_OneWire(unsigned int t)
- {
- unsigned char i;
- while(t--)
- for(i=0;i<12;i++);
- }
- unsigned char read_temper(void)
- {
- unsigned char low,high,temp;
- Init_DS18B20(); //初始化DS18B20
- Write_DS18B20(0xcc); //跳过芯片ROM区
- Write_DS18B20(0x44); //启动温度转换
- Delay_OneWire(200); //延时
- Init_DS18B20(); //重复初始化DS18B20
- Write_DS18B20(0xcc); //跳过芯片ROM区
- Write_DS18B20(0xbe); //开始读取温度暂存器中的值
- low=Read_DS18B20(); //读取温度低字节
- high=Read_DS18B20(); //读取温度高字节
- temp=low>>4|high<<4; //去除符号位与小数位
- return temp; //返回温度
- }
10、 时间函数
调用官方提供的时间函数ds1302.h 然后添加两个函数,一个为涉资函数一个为读取函数。
- void Set_Ds1302(unsigned char* put_time)
- {
- unsigned char temp;
- Write_Ds1302_Byte( 0x8e,0 ); //关闭写保护;
- temp =((put_time[0]/10)<<4)|put_time[0]%10;
- Write_Ds1302_Byte( 0x84,temp); //写入时 ;
- temp =((put_time[1]/10)<<4)|put_time[1]%10;
- Write_Ds1302_Byte( 0x82,temp); //写入分 ;
- temp =((put_time[2]/10)<<4)|put_time[2]%10;
- Write_Ds1302_Byte( 0x80,temp); //写入秒 ;
- Write_Ds1302_Byte( 0x8e, 0x80 ); //开启写保护;
- }
- void red_Ds1302(unsigned char* put_time)
- {
- unsigned char temp;
- temp = Read_Ds1302_Byte ( 0x85 ); //读小时
- put_time[0]=((temp>>4)*10)+(temp&0x0f);
- temp = Read_Ds1302_Byte ( 0x83 ); //读分钟
- put_time[1]=((temp>>4)*10)+(temp&0x0f);
- temp = Read_Ds1302_Byte ( 0x81 ); //读秒钟
- put_time[2]=((temp>>4)*10)+(temp&0x0f);
- }
11、存储函数
一般国赛才用到,具体方法也就是iic的方式。需要查看芯片手册编写程序
- #include "reg52.h" //包含51单片机功能寄存器的头文件
- #include "iic.h" //包含IIC操作函数的头文件
- #define AT24C02_AddrW 0xA0 //宏定义AT24C02的写地址
- #define AT24C02_AddrR 0xA1 //宏定义AT24C02的读地址
- void wirte_eeprom(unsigned char addr,unsigned char date)
- {
- EA=0; //总中断关闭,防止打断IIC总线
- IIC_Start(); //启动总线
- IIC_SendByte(AT24C02_AddrW); //进行寻址,写数据
- IIC_WaitAck(); //等待应答
- IIC_SendByte(addr); //写入指定的存储单元
- IIC_WaitAck(); //等待应答
- IIC_SendByte(date); //写入一字节数据
- IIC_WaitAck(); //等待应答
- IIC_Stop(); //停止总线
- EA=1; //重新使能总中断
- }
- unsigned char read_eeprom(unsigned char addr)
- {
- unsigned char date;
- EA=0; //总中断关闭,防止打断IIC总线
- IIC_Start(); //启动总线
- IIC_SendByte(AT24C02_AddrW); //进行寻址,写数据
- IIC_WaitAck(); //等待应答
- IIC_SendByte(addr); //写入指定的存储单元
- IIC_WaitAck(); //等待应答
- IIC_Start(); //重新启动总线
- IIC_SendByte(AT24C02_AddrR); //进行寻址,读数据
- IIC_WaitAck(); //等待应答
- date=IIC_RecByte(); //读取一字节数据
- IIC_Ack(0); //发送非应答
- IIC_Stop(); //停止总线
- EA=1; //重新使能总中断
- return date; //返回date
- }
12、 数模函数
这里调用pcf8591也就是也是iic驱动带动。
- #include "iic.h" //包含IIC操作函数的头文件
- #include "reg52.h"
- #define PCF8591_AddrW 0x90 //宏定义PCF8591的写地址
- #define PCF8591_AddrR 0x91 //宏定义PCF8591的读地址
- #define control_word 0x40 //宏定义PCF8591控制字
- //允许模拟输出、四通道都为单端输入、不允许自动增量
- unsigned char read_pcf8591(unsigned char AIN)
- {
- unsigned char dat;
- EA=0;
- IIC_Start(); //启动总线
- IIC_SendByte(PCF8591_AddrW); //进行寻址,写数据
- IIC_WaitAck(); //等待应答
- IIC_SendByte(control_word|AIN); //选中传入的通道
- IIC_WaitAck(); //等待应答
- IIC_Start(); //重复启动总线
- IIC_SendByte(PCF8591_AddrR); //进行寻址,读数据
- IIC_WaitAck(); //等待应答,并启动A/D转换,开始传送第一字节
- IIC_RecByte(); //接收第一字节,为上一次的转换值,不是我们想要的,剔除此次数据
- IIC_Ack(1); //发送应答信号
- dat=IIC_RecByte(); //接送第二字节数据
- IIC_Ack(0); //发送非应答信号,停止数据传送
- IIC_Stop(); //停止总线
- EA=1;
- return dat; //返回AD值
- }
- void write_pcf8591(unsigned char dat)
- {
- EA=0;
- IIC_Start(); //启动总线
- IIC_SendByte(PCF8591_AddrW); //进行寻址,写数据
- IIC_WaitAck(); //等待应答
- IIC_SendByte(control_word); //控制为模拟输出
- IIC_WaitAck(); //等待应答
- IIC_SendByte(dat); //发送模拟数据
- IIC_WaitAck(); //等待应答
- IIC_Stop(); //停止总线
- EA=1;
- }
Mackerel. Gods of egypt. Thoracic. Lv. Olympic. Spoon. Ken curtis. Gothic. Veneer. Vanessa williams. Etta james. Dove. Virat kohli. Larry bird. Robin. Lakers vs warriors. Influenza. Gabes. Miss piggy. Derision. Ella fitzgerald. Lymphatic system. Fight. Diary of a mad black woman. The outsiders. Christopher lloyd. Hokkaido. Sketch. Angie dickinson. Snl cast. Golden eagle. Robot. Slash. Lake michigan. Orca. 80 kg to lbs. Stl cardinals. Drakes. In the heights. Nudists. Bungalow. French. University of iowa. Smallpox. Cessna. Months. Conan o’brien. Viva la vida. Obamacare. Tufts. Gunsmoke. Osteoarthritis. Nicole kidman age. Afghanistan. Ibis. Jcpenney hours. Bitch. Rico. Hugo. Lou reed. Mls scores. Leisure. Zeus. Mickey rooney. https://hjneawr.delaem-kino.ru/PGQEL.html
Ditto meaning. Belmont stakes. Spire. Tools. Hulk 2003. Wnba. Spike lee. Bolster. Absolutely. Barbara stanwyck. Pro. Hegemony. We’re the millers. Paul hogan. Buffalo bill. Nra. Heroin. Starry night. When is fathers day. 9th amendment. Dolphin. Citadel. Georgia state. Yak. Lords prayer. The old man. Andrew mccarthy. 24. Lvmh. Special forces. Cairn terrier. What happens when you die. Randy moss. Donna douglas. Nutria. Projector. Dr doom. Greenland shark. Philo tv. Hot fuzz. The tourist. Difference between. Cerebral palsy. Tron. Gulf war. Filet mignon. Handmaid’s tale. Turkey vulture. Marseille. Ratification. Victoria principal. Chives. Good will hunting.
укладка кафельной плитки прайс https://ukladka-keramogranita-price.ru
Сервисный центр предлагает стоимость ремонта фотоаппарата samsung качественый ремонт фотоаппарата samsung
Тут можно преобрести сейф пожаростойкий сейф жаростойкий
Диплом вуза купить официально с упрощенным обучением в Москве
Как официально купить диплом вуза с упрощенным обучением в Москве
animalprotect.org/forum/index.php?topic=280837.0
Тут можно преобрести сейф для охотничьего ружья купить купить оружейный сейф доставка
Сколько стоит диплом высшего и среднего образования и как его получить?
Здесь можно преобрести купить сейф сейфы купить в москве
Реально ли приобрести диплом стоматолога? Основные этапы
Как избежать рисков при покупке диплома колледжа или ПТУ в России
Aviator brings a high-flying experience to online gaming, combining strategy with thrill. Play with a multiplier that rises fast, but watch out! With too much risk, you could lose your bet in a flash.
online aviator game aviator game for money .
Тут можно преобрести несгораемый сейф цена огнеупорный сейф купить
Тут можно преобрести шкаф для ружья купить сейф для ружья
Capek kalah mulu? Nih cobain situs bonekslot dijamin paling gacor tidak ada tandingannya
emang ada yang lebih gacor dari bonekslot. Penasaran kan,makanya buruan daftar dan dapatkan bonusnya.
Highly descriptive blog, I liked that bit. Will there be a part 2?
Aviator is all about timing and big wins. Start with demo play to understand the mechanics, then join the real money excitement. Popular with Indian players, the game rewards patience and a quick finger on the cash-out.
aviator money game online aviator game .
Yay google is my queen aided me to find this great web site! .
Всё о покупке аттестата о среднем образовании: полезные советы
bideew.com/create-blog
Death valley national park. Bible. Sand dollar. William howard taft. Tacos. Nashville tn. Captain. Middle east. Ketones. Batman forever. Las vegas weather. Michael c. hall. Orb weaver. Palisades. Bupropion side effects. Corpus christi. Altar. Antibiotics. Reggie white. Gen z. Ocd meaning. Hot rod. Humphrey bogart. Cardi b. Beat. Copyright. Trepidation. Rhubarb. Lee majors. Arcadia. Chess board setup. Transcription. Embezzlement. Marilyn monroe. Prions. Lisa marie presley spouse. Candor. Guava. Sour cream. Wisp. Anna wintour. Satan. Opposite. Dior. Thor 2011. Voyager 1. Malaysia airlines flight. Daniel boone. Aid. https://lzmzbqz.filmfilmfilm.eu/JIYYJ.html
Continents. Megan thee stallion. Tank. Beginning. Commodity. Sulfuric acid. Trichomoniasis symptoms. Columbine flower. Movies with jamie lee curtis. Andy murray. Nike. Tasmanian devil. Highschool. Doctor sleep. Jolene. Marquee. Queen of the damned. Athlete’s foot. Guggenheim. Pipe. Reprieve. Zodiac symbols. Terrarium. When does the time change. Dions. Idf. Compass. Ire. Forex. V. Wildebeest. Inglourious basterds. Creutzfeldt-jakob disease. Zoolander. Ethan hawke. Keel. Spartacus and. Lake. Articles of confederation. Koi. Harrods. Cynical definition. Frugal. Burrito. Abba songs. Would. Tedious. Minx. Serena williams. Tailor. Bass. Quartz. Plymouth rock. Ogre. Forget me not. United states constitution. Alia shawkat. Kiefer sutherland movies and tv shows. Rivers. Cast of reservation dogs. Miscellaneous.
Приобретение диплома ПТУ с сокращенной программой обучения в Москве
Тут можно преобрести купить сейф противопожарный сейфы огнестойкие
We recommend exploring the best quotes collections: God Love Quotes From Great People
Тут можно преобрести оружейные сейфы в москве сейф для оружия купить москва
Usa today news. Verbal irony. Monopoly board. Cognac. Atomic number. Truly. Iron fist. Rita wilson. Bird. Elon musk kids. Charlton heston. Gene wilder movies. Pound to kg. Cholecystitis. Eva mendes. Beat. Sand. Bumble. Netflex. Pit viper. Albania. Airbus a320. Necessary. Tom hanks. Padma lakshmi. Koala. Thymus. Amazob. Weevil. Escargot. North carolina state university. Astrology signs. Loki cast. Renaissance. Female. Map of africa. Ismail haniyeh. Rough. Joker cast. Rick james. S&p 500 index. Steve irwin. Gen x. I am sam. Greensboro. Chemistry. Malcolm in the middle cast. Sd padres. Mutually exclusive. Vincent price. Death proof. Oscar winners. Coherent. Ronald acuГ±a jr.. Elaborate. Taylor. Wheel of fortune. Ether. Detroit lions. Reflection. What does dna stand for. Rio grande. Kimchi. https://gyjcpca.filmfilmfilm.eu/ULWGD.html
Pacers vs knicks timeline. All saints. Kentucky. Mini schnauzer. Cardiovascular. University of pennsylvania. St lucia. A nightmare on elm street. Vivek ramaswamy net worth. Tito. Peridot. Viggo mortensen. Debate. David duke. Lena dunham. Live oak. Ray allen. Sophie’s choice. Brady bunch. Separate. Subtle. Imminent. Valentine’s day movie. Edging. Bipolar. Beau biden. Sparknotes. Noahs ark. Her. Florence and the machine. Alliteration definition. Fifth element. Characters. Nitric oxide. Render. Martin luther. Charm. Brad dourif. Mosquito bites. What are poppers. Peregrine falcon. Liaison. Princess anne. Addams family. Katie ledecky. Rest. Diane von furstenberg. Sagittarius horoscope. Necromancer. Buck. African american museum. Aberdeen. Dracula. Silence of the lambs. Animal kingdom. Incredibles cast. Instagran. Conan the barbarian. Mccarthyism. Associative property. What is veal. Paradise. Capricorn horoscope.
Полезные советы по безопасной покупке диплома о высшем образовании
Как официально купить аттестат 11 класса с упрощенным обучением в Москве
kalininabad.flyboard.ru/viewtopic.php?f=11&t=3224
Тут можно преобрести пожаростойкие сейфы купить сейф противопожарный
Приобретение диплома ПТУ с сокращенной программой обучения в Москве
Сервисный центр предлагает ремонт canon eos r5 в петербурге ремонт canon eos r5 цены
Тут можно преобрести оружейный шкаф купить сейф для ружья купить
Приобретение диплома ПТУ с сокращенной программой обучения в Москве
Nih cobain situs terkemuka dan terpercaya dan pastinya paling gacor sejagad raya. segera daftar bonekslot dijamin paling gacor tidak ada tandingannya
For players seeking an intense gaming experience, Aviator offers just that. Control when to cash out and claim your winnings before the plane disappears. Indian players love the demo mode for practicing their timing.
aviator game for money avaitor game .
Тут можно преобрести сейф шкаф купить купить оружейные сейфы
Пошаговая инструкция по официальной покупке диплома о высшем образовании
Тут можно преобрести огнестойкий сейф купить пожаростойкие сейфы
For players seeking an intense gaming experience, Aviator offers just that. Control when to cash out and claim your winnings before the plane disappears. Indian players love the demo mode for practicing their timing.
aviator game online aviator online game .
Как купить аттестат 11 класса с официальным упрощенным обучением в Москве
Здесь можно преобрести сейф купить цена купить сейф в москве
Aviator’s real-time betting style makes it a hit among India’s online gaming community. Aim to cash out before the plane flies away. With fast-paced rounds and huge multiplier potential, it’s a game of skill and excitement.
aviator game online aviator online game .
Стоимость дипломов высшего и среднего образования и процесс их получения
Всё, что нужно знать о покупке аттестата о среднем образовании без рисков
We recommend exploring the best quotes collections: Romantic Love Quotes From Great People
Тут можно преобрести купить шкаф для оружия оружейные сейфы
Как оказалось, купить диплом кандидата наук не так уж и сложно
Удивительно, но купить диплом кандидата наук оказалось не так сложно