PIC12F508单⽚机使⽤教程
1.安装MPLAB X IDE与XC8 C语⾔编译器
2.配置IDE使⽤XC8 C语⾔编译器
双击IDE快捷⽅式,打开菜单:Tools>Options>Embedded>Build Tools.
然后按下图添加与配置IDE使⽤XC8 编译器:
此时开发环境已搭建完毕!
3.新建⼀个⼯程
3.1、新建⼯程的3种⽅法:
3.2、选择“Standalone Pronject”,点击Next:
3.3、选择单⽚机类型和型号,点击Next:
3.4、默认选项,直接点击Next
3.5、选择编程器,根据个⼈需求选择,也可以任意选择⼀个,后续再根据需求修改,这⾥选择“PICkit3”,点击Next:
3.6、选择编译器,这⾥选择上⾯安装的XC8编译器,点击Next:
3.7、填写⼯程名字和选择⼯程保存路径,点击Finish即可新建完⼀个⼯程
4、PIC12F508功能位在IDE的配置
贴⼀个我写的代码:
/*
* File:  main.c
* Author: Administrator
*
* Created on April 20, 2020, 11:30 AM
*/
#include<xc.h>//适⽤于所有PIC 8位单⽚机,包含了所有PIC 8位单⽚机的头⽂件
#include<stdint.h>
// CONFIG
#pragma config OSC = IntRC      // Oscillator Selection bits (internal RC oscillator)
#pragma config WDT = OFF        // Watchdog Timer Enable bit (WDT disabled)
#pragma config CP = ON          // Code Protection bit (Code protection on)
#pragma config MCLRE = OFF      // GP3/MCLR Pin Function Select bit (GP3/MCLR pin function is digital input, MCLR internally tied to VDD)
int tc =0;
uint8_t dice_start =1;
uint8_t dice_data[6]={1,2,3,4,5,6};
uint8_t flag =0;
uint8_t start_time =0;
//uint8_t stop_time = 0;
///
void pic12f508_init(void){
OPTION =0xd7;//time0 select internal clock,Fosc/4, prescale=1:256
TRISGPIO =0x08;//G0,G1,G2,G4,G5 as output mode,G3 as input mode
GPIO =0x00;//turn off all led
TMR0 =0x3f;//initialization timer,timing 0.05second
}
///
void check_time(void){
if(TMR0 ==0xff){
TMR0 =0x3f;//initialization timer
if(++tc ==3600){//timing 180second
tc =0;
}
}
}
}
///
void led_display(char dat){
switch(dat){
case0:GP0=0;GP1=0;GP2=0;GP4=0;break;
case1:GP0=0;GP1=1;GP2=0;GP4=0;break;
case2:GP0=0;GP1=0;GP2=0;GP4=1;break;
case3:GP0=0;GP1=1;GP2=1;GP4=0;break;
case4:GP0=0;GP1=0;GP2=1;GP4=1;break;
case5:GP0=0;GP1=1;GP2=1;GP4=1;break;
case6:GP0=1;GP1=0;GP2=1;GP4=1;break;
default:break;
}
}
///
void delay(int t){
uint8_t a=0,b=0;
while(t>1){
for(a=0;a<200;a++){
while(b<200){b++;}c语言编译器ide代码编辑
}
t--;
}
}
///
void main(void){
pic12f508_init();
while(1){
check_time();
if(GP3 ==1&& dice_start ==1){
dice_data[5]= dice_data[4];
dice_data[4]= dice_data[3];
dice_data[3]= dice_data[2];
dice_data[2]= dice_data[1];
dice_data[1]= dice_data[0];
do{
dice_data[0]=(TMR0 -0x3f)%7;
}while(dice_data[0]==0);
led_display(dice_data[2]);
dice_start =0;
start_time = tc;
flag =1;
}
if(GP3 ==0&& dice_start ==0){
dice_start =1;
start_time = tc;
flag =1;
}
char time =0;
time = tc - start_time;
if(15< time && time <25&& flag ==1){
uint8_t i;
for(i=6;i>0;i--){
led_display(dice_data[i-1]);
delay(80);
}
//led_display(dice_data[0]);
flag =2;
}
time = tc - start_time;
if(time >60&& flag ==2){
uint8_t c;
for(c=0;c<5;c++){
led_display(0);

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。