第二天开始第一个程序
今天的任务,点亮一个LED,和编写一个流水灯程序。我们直接进入主题,先介绍下PIC单片机I/O口,I/O端口的寄存器复位后,默认为输入(输出高阻态),每一路I/O有方向控制寄存器TRISA~E和数据寄存器PORTA~E,为了实现I/O端口的功能必需先对端口进行初始化设置。 方向控制寄存器与数据寄存器都是8位的,方向控制寄存器中的每一位与数据寄存器中的每一位相对应;方向控制寄存器的相应位设置为1表示输入;设置为0表示输出;端口控制寄存器的相应位设置为1表示输出高电平,设置为0表示输出低电平。 这个需要我们记住。通用I/O接口的输出设计要点:1、输出电平的转换和匹配,我们PIC系统一般工作电源是5V,当连接的外围是9V、12V等于5V不同的电源时,需要考虑输出电平的转换。2、输出电流的驱动能力,每个I/O口允许最大25mA的灌电流和20mA的拉电流,可以直接驱动LED和继电器。因各端口结构不同,RB口提供的总灌电流和总拉电流可达150mA和100mA;其他端口为80mA和50mA;五个端口驱动电流之和不大于200mA。3、输出电平转换的延时,PIC单片机是一款高速单片机,当系统晶振为4MHZ时,执行一个指令的时间为0.25us,也就是说讲一个I/O置1,在置0,只需要0.25us。
- LED发光二级管电流硬件设计:二极管电流大于5MA,人眼睛就可以明显的观察到二极管的发光,导通电流越大,亮度越高。但最好不要超过10ma,否则可能会将二级管烧坏或IO口烧坏。我们在设计电路时,需要在电路中串接一个限流电阻,这个电流一般在330-1K之间。
- 软件设计,代码如下:
#include<pic.h> //头文件包含
//芯片配置字定义
#pragma config FOSC = XT // Oscillator Selection bits (XT oscillator: Crystal/resonator on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON // RE3/MCLR pin function select bit (RE3/MCLR pin function is MCLR)
#pragma config CP = OFF // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = ON // Brown Out Reset Selection bits (BOR enabled)
#pragma config IESO = ON // Internal External Switchover bit (Internal/External Switchover mode is enabled)
#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is enabled)
#pragma config LVP = ON // Low Voltage Programming Enable bit (RB3/PGM pin has PGM function, low voltage programming enabled)
#pragma config BOR4V = BOR40V // Brown-out Reset Selection bit (Brown-out Reset set to 4.0V)
#pragma config WRT = OFF // Flash Program Memory Self Write Enable bits (Write protection off)
void main()
{
// TRISC=0xfe;//11111110,端口操作
// PORTC=0xfe;//00000001
TRISC0=0;//位操作,类似51单片机sbit
RC0=0;
while(1);
}
实验效果如下:
流水灯程序如下:
#include<pic.h>
#define uchar unsigned char
#define uint unsigned int
#pragma config FOSC = XT // Oscillator Selection bits (XT oscillator: Crystal/resonator on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON // RE3/MCLR pin function select bit (RE3/MCLR pin function is MCLR)
#pragma config CP = OFF // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = ON // Brown Out Reset Selection bits (BOR enabled)
#pragma config IESO = ON // Internal External Switchover bit (Internal/External Switchover mode is enabled)
#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is enabled)
#pragma config LVP = ON // Low Voltage Programming Enable bit (RB3/PGM pin has PGM function, low voltage programming enabled)
#pragma config BOR4V = BOR40V // Brown-out Reset Selection bit (Brown-out Reset set to 4.0V)
#pragma config WRT = OFF // Flash Program Memory Self Write Enable bits (Write protection off)
void delay_ms(uint x)
{
uint a,b;
for(a=x;a>0;a--)
for(b=110;b>0;b--);
}
void main()
{
uchar position=0;
TRISC=0x00;//设置为输出方式
while(1)
{
PORTC=~(1<<postion);
if(++position>=8) postion=0;
delay_ms(1000);
}
}
|