Microchip PIC系列8位单片机入门教程(2):点亮LED灯

01第一节 配置字
前言:器件选择pic18f4520
(1)配置字的含义:pic系列的单片机都有配置字,这是从宏观设置单片机功能的寄存器,我们不需要手动配置。
首先我们新建一个源文件main.c
在包含头文件的前面单击,让光标在前面。
在工具栏中点击production->set configutation bits
配置好之后点击红色框就将配置的信息插入到main.c 的头文件包含语句的前面了。这个必须在包含头文件的语句之前。
(2)配置字的解释:
// config1h#pragma config osc = hs // oscillator selection bits (hs oscillator)--振荡器选择位(hs振荡器)#pragma config fcmen = off // fail-safe clock monitor enable bit (fail-safe clock monitor disabled)--失效保护时钟监视器启用位(失效保护时钟监视器禁用)#pragma config ieso = off // internal/external oscillator switchover bit (oscillator switchover mode disabled)--内部/外部振荡器切换位(振荡器切换模式禁用)// config2l#pragma config pwrt = off // power-up timer enable bit (pwrt disabled)--通电定时器启用位(pwrt禁用)#pragma config boren = sbordis // brown-out reset enable bits (brown-out reset enabled in hardware only (sboren is disabled))--失电输出复位启用位(仅在硬件中启用失电输出复位(禁用sboren))#pragma config borv = 3 // brown out reset voltage bits (minimum setting)--失电复位电压位(最小设置)// config2h#pragma config wdt = off // watchdog timer enable bit (wdt disabled (control is placed on the swdten bit))#pragma config wdtps = 32768 // watchdog timer postscale select bits (1:32768)--看门狗定时器启用位(wdt禁用(控制置于swdten位))// config3h#pragma config ccp2mx = portc // ccp2 mux bit (ccp2 input/output is multiplexed with rc1)--看门狗定时器启用位(wdt禁用(控制置于swdten位上))ccp2 mux位(ccp2输入/输出与rc1多路复用)#pragma config pbaden = off // portb a/d enable bit (portbpins are configured as analog input channels on reset)--端口b a/d启用位(端口b引脚在复位时配置为模拟输入通道)#pragma config lpt1osc = off // low-power timer1 oscillator enable bit (timer1 configured for higher power operation)--端口b a/d启用位(复位时端口b引脚配置为模拟输入通道)低功率定时器1振荡器启用位(定时器1配置为高功率操作)#pragma config mclre = off // mclr pin enable bit (re3 input pin enabled; mclr disabled) ---mclr引脚启用位(re3输入引脚启用;mclr禁用)// config4l#pragma config stvren = off // stack full/underflow reset enable bit (stack full/underflow will not cause reset)--堆栈满/下溢复位启用位(堆栈满/下溢不会导致复位)#pragma config lvp = off // single-supply icsp enable bit (single-supply icsp disabled)--单电源icsp启用位(单电源icsp禁用)#pragma config xinst = off // extended instruction set enable bit (instruction set extension and indexed addressing mode disabled (legacy mode))--扩展指令集启用位(禁用指令集扩展和索引寻址模式(传统模式))// config5l#pragma config cp0 = off // code protection bit (block 0 (000800-001fffh) not code-protected)--代码保护位(块0(000800-001fffh)不受代码保护)#pragma config cp1 = off // code protection bit (block 1 (002000-003fffh) not code-protected)--代码保护位(块1(002000-003fffh)不受代码保护)#pragma config cp2 = off // code protection bit (block 2 (004000-005fffh) not code-protected)--代码保护位(块2(004000-005fffh)不受代码保护)#pragma config cp3 = off // code protection bit (block 3 (006000-007fffh) not code-protected)--代码保护位(块3(006000-007fffh)不受代码保护)// config5h#pragma config cpb = off // boot block code protection bit (boot block (000000-0007ffh) not code-protected)--引导块代码保护位(引导块(000000-0007ffh)不受代码保护)#pragma config cpd = off // data eeprom code protection bit (data eeprom not code-protected)--数据eeprom代码保护位(数据eeprom不受代码保护)// config6l#pragma config wrt0 = off // write protection bit (block 0 (000800-001fffh) not write-protected)--写保护位(块0(000800-001fffh)未写保护)#pragma config wrt1 = off // write protection bit (block 1 (002000-003fffh) not write-protected)--写保护位(块1(002000-003fffh)未写保护)#pragma config wrt2 = off // write protection bit (block 2 (004000-005fffh) not write-protected)--写保护位(块2(004000-005fffh)不写保护)#pragma config wrt3 = off // write protection bit (block 3 (006000-007fffh) not write-protected)--写保护位(块3(006000-007fffh)未写保护)// config6h#pragma config wrtc = off // configuration register write protection bit (configuration registers (300000-3000ffh) not write-protected)--配置寄存器写保护位(配置寄存器(300000-3000ffh)不写保护)#pragma config wrtb = off // boot block write protection bit (boot block (000000-0007ffh) not write-protected)--引导块写保护位(引导块(000000-0007ffh)未写保护)#pragma config wrtd = off // data eeprom write protection bit (data eeprom not write-protected)--数据eeprom写保护位(数据eeprom不写保护)// config7l#pragma config ebtr0 = off // table read protection bit (block 0 (000800-001fffh) not protected from table reads executed in other blocks)--表读取保护位(块0(000800-001fffh)不受其他块中执行的表读取的保护)#pragma config ebtr1 = off // table read protection bit (block 1 (002000-003fffh) not protected from table reads executed in other blocks)--表读取保护位(块1(002000-003fffh)不受其他块中执行的表读取的保护)#pragma config ebtr2 = off // table read protection bit (block 2 (004000-005fffh) not protected from table reads executed in other blocks)--表读取保护位(块2(004000-005fffh)不受其他块中执行的表读取的保护)#pragma config ebtr3 = off // table read protection bit (block 3 (006000-007fffh) not protected from table reads executed in other blocks)--表读取保护位(块3(006000-007fffh)不受在其他块中执行的表读取的保护)// config7h#pragma config ebtrb = off // boot block table read protection bit (boot block (000000-0007ffh) not protected from table reads executed in other blocks)--引导块表读取保护位(引导块(000000-0007ffh)不受在其他块中执行的表读取的保护)// #pragma config statements should precede project file includes.--pragma config语句应该在project file includes之前// use project enums instead of #define for on and off.我们主要选择配置好时钟、关闭看门狗,其他的默认就可以了。如果在产品中要保护代码段可以根据具体要求设置,使用adc是要打开pbaded on;
02第二节 代码编写
我们在main.c 中添加宏:
#define _xtal_freq 40000000 //定义时钟为40mhz#define led_port_dir trisd#define led_port_datd latdconst unsigned char led[8]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};第一个宏是定义时钟的频率,第二是定义led灯的端口方向寄存器,第三个是定义端口的寄存器。
第一个函数:初始化控制led灯的端口,配置为输出方向,初始值为0;
第四个是变量,实现流水灯的。
void pic18_led_port_init(void){ led_port_dir=0; //setting output direction for led stream . led_port_datd=0; // init led port data to low voltage.}第二个函数是实现流水灯:
void led_stream_show(void){ unsigned char i; for(i=0;i< 8;i++) { latd=led[i]; __delay_ms(1000); //延时1s } latd=0xff; __delay_ms(1000); latd=0; __delay_ms(1000);}使用_delayms(1000);这个是系统函数,前面定义了时钟频率,就可以直接使用了。
我们编写main函数看效果:
void main(void){ pic18_led_port_init(); while(1) { led_stream_show(); }}
因为手头开发板不在,所以在proteus中portd端口安放了8个led灯仿真了下,是可以的。
03总结
led灯的实现主要使用了单片机的i/o 端口,方向寄存器设置为0,输出,在portd端口的数据锁存器写入值就可以控制led灯的亮灭。
从图中就可以看出,如果要读portd端口的值则要使用portd寄存器。写使用latd。再加一点延迟函数,就实现流水灯的效果。

苹果取消打赏抽成!苹果取消微信内容打赏抽成的背后,是屈服还是套路?
人工智能会有意识吗
混合硬盘介绍
EMI实际应用解决方案的学习
开关电源TL431环路接法及传递函数推导
Microchip PIC系列8位单片机入门教程(2):点亮LED灯
CAD-CAM数据转换的新趋势
思科CEO预测联网设备增速超预期 网络安全受关注
中国联通已经开启第二轮机构缩减联通怎么渡过难关
有个创基USB集线器文档交换一键完成
Science Robotics揭示未来机器人领域的走向
矿机真的不好卖了 比特大陆陷财务困局矿机暴跌90%
晶振与晶体的区别
全球CMOS图像传感器销售额年年创新高_新兴应用持续涌现
OPPO一款新机曝光将搭载屏下摄像头
UWB定位技术助力煤矿智能化发展
一种基于GPS的数据采集处理系统的研究设计
基于FPGA的全光纤电流互感器控制电路设计
空气流量传感器工作原理
无人机出口到日本的是做无线telec认证吗