PIC16lF1936编程仿真及PIC触摸库
扫描二维码
随时随地手机看文章
目录:
Mplab IDE的使用
一、接入PICkit3出现Target Device ID (00000000) does not match expected Device ID (00002460)
二、首次使用pickit3出现PK3Err0040
三、接入PICkit3烧录时要设置电源
四、maplab的输出窗口看不见
Mplab X IDE的使用
一、接入PICkit3提示找不到工具,为驱动没有安装
二、microchip官网下载XC8编译器
实践
一、端口控制
二、实现触摸功能
三、使用PIC公司的触摸库做触摸处理
C与汇编混合编程
--------------------------------------------------------------------------------------------------------------
看到PIC单片机就感到亲切,今天开始对PIC16lf1936编程仿真,仿真器PICKIT3。
--------------------------------------------------------------------------------------------------------------
Mplab IDE的使用
一、接入PICkit3出现Target Device ID (00000000) does not match expected Device ID (00002460)
这是仿真头与线路板连接不正确。
二、首次使用pickit3出现PK3Err0040
PK3Err0040: The target device is not ready for debugging.Please check your configuration bit settings and program the device before proceeding.
解决办法:在这里勾 选:Debugger>Settings>Program Memory>Automatically 的 “Program after successful build”项即可在每次成功后自动下载代码到Flash并进入调试就绪模式。
三、接入PICkit3烧录时要设置电源
PK3Err0045: You must connect to a target device to use PICkit3.
Programmer>Settings>Power中设置
四、maplab的输出窗口看不见
1、在设置这个功能的时候,准备把output窗口移到下边缘,没想到移动过头,移动到电脑桌面的最下方(移到下发看不到的位置去了),然后output窗口就不见了,调不出来,在view里面勾选了output或者取消,编译文件,甚至重新启动电脑,output窗口都不出现了!
右击任务栏->解除锁定任务栏->拖动任务栏到右边就可以看到被隐藏的窗口了
2、隐藏在整个窗口的下方
右侧是有进度条的。
将output移上去,右侧就没有进度条了。
--------------------------------------------------------------------------------------------------------------
Mplab X IDE的使用
图1 驱动切换与安装
图2 批量烧录
一、接入PICkit3提示找不到工具,为驱动没有安装
打开如图1 MPLAB drive,如下图设置,点"Apply Changes"即可。
二、microchip官网下载XC8编译器
--------------------------------------------------------------------------------------------------------------
实践
一、端口控制
完成三件事:1、仿真部分全部正常;2、配置字设置无误;3、内部晶振正常,端口操作正常
#include "pic.h"
void delay_long(unsigned int uiDelayLong);
void main()
{
OSCCON = 0xeb;//禁止4X PLL,内部4MHz时钟源
TRISBbits.TRISB3 = 0;//设置RB5端口为输出
TRISCbits.TRISC4 = 0;
ANSELBbits.ANSB3 = 0;//RB5设置为数字端口
LATBbits.LATB3 = 0;//RB3输出低电平
LATCbits.LATC4 = 0;
WPUBbits.WPUB5 = 1;
while (1)
{
LATBbits.LATB3 = 1;
LATCbits.LATC4 = 0;
delay_long(100);
LATBbits.LATB3 = 0;
LATCbits.LATC4 = 1;
delay_long(100);
}
}
void delay_long(unsigned int uiDelayLong)
{
unsigned int i;
unsigned int j;
for(i=0;i
for(j=0;j<500;j++)//内嵌循环的空指令数量
{
;
}
}
}
/////////////////////////////////////////////////////////////////////////////
二、实现触摸功能
#include "pic.h"
__CONFIG(0x09E4);
__CONFIG(0x0033);
#define BackLight LATCbits.LATC4
#define Light1 LATAbits.LATA7
#define Light2 LATBbits.LATB3
#define Light3 LATAbits.LATA4
#define Light4 LATBbits.LATB4
#define L2_CLATCbits.LATC0
#define L1_CLATCbits.LATC1
#define C_RLATCbits.LATC2
#define C_LLATCbits.LATC3
#define Cap_H_L LATAbits.LATA6
#define const_key_time2 8
#define const_timr0_value 100
#define trip_value 150//(未触摸值-触摸值)*80%
#define Cap_Avg_value 1500
void key_service(void);
void delay_long(unsigned int uiDelayLong);
static void interrupt SystemISR(void);
void System_Init(void);
void Cap_Init(void);
void SetNextSensor(void);
void RestartTimers(void);
void Cap_ISR(void);
unsigned char Index = 1;// 触摸键通道计数
unsigned char AvgIndex = 0;//更新频率计数
unsigned int Cap_Value = 0;//当前按键频率值
const unsigned int trip[4]={trip_value,trip_value,trip_value,trip_value}; //频率差值
unsigned int Cap_Avg[4]={Cap_Avg_value,Cap_Avg_value,Cap_Avg_value,Cap_Avg_value}; //频率平均值
unsigned int uiKeyTimeCnt1=0;
unsigned int uiKeyTimeCnt2=0;
unsigned int uiKeyTimeCnt3=0;
unsigned int uiKeyTimeCnt4=0;
unsigned char ucKeyLock1=0;
unsigned char ucKeyLock2=0;
unsigned char ucKeyLock3=0;
unsigned char ucKeyLock4=0;
unsigned char ucKey1TouchCnt=0;
unsigned char ucKey2TouchCnt=0;
unsigned char ucKey3TouchCnt=0;
unsigned char ucKey4TouchCnt=0;
unsigned char ucKeySec=0;
void main()
{
System_Init();
delay_long(10);
Cap_Init();
GIE = 1;
while (1)
{
key_service();
}
}
void key_service(void)
{
switch(ucKeySec)
{
case 1:
{
}
break;
case 2:
{
if(ucKey2TouchCnt == 1)
{
Light2 = 0;
C_L = 1;
}else
{
Light2 = 1;
C_L = 0;
ucKey2TouchCnt = 0;
}
}
break;
case 3:
{
}
break;
case 4:
{
}
break;
default:
break;
}
}
void delay_long(unsigned int uiDelayLong)
{
unsigned int i,j;
for(i=0;i
for(j=0;j<500;j++)
{
asm("nop");
}
}
}
void System_Init(void)
{
OSCCON = 0b01101000;//内部4M晶振
OSCTUNE= 0b00000000;//晶振校准
TRISCbits.TRISC0 = 0;
TRISCbits.TRISC1 = 0;
TRISCbits.TRISC2 = 0;
TRISCbits.TRISC3 = 0;
L2_C = 0;
L1_C = 0;
C_R = 0;
C_L = 0;
TRISAbits.TRISA6 = 0;
LATAbits.LATA6 = 0;
TRISCbits.TRISC4 = 0;//BackLight输出
TRISAbits.TRISA7 = 0;//Light1输出
TRISBbits.TRISB3 = 0;//Light2输出
TRISAbits.TRISA4 = 0;//Light3输出
TRISBbits.TRISB4 = 0;//Light4输出
TRISBbits.TRISB1 = 1;//RB1触摸按键输入
ANSELBbits.ANSB1 = 1;//RB1设置为模拟端口
WPUB = 0x00; //PORTB口无上拉
IOCBP = 0x00; //关PORTB口电平变化中断
IOCBN = 0x00;
BackLight = 0; //背景灯打开
Light1 = 1;//其它灯关闭
Light2 = 1;
Light3 = 1;
Light4 = 1;
}
void Cap_Init(void)
{
CPSCON0 = 0b10001100; //打开电容模块,高频率振荡
CPSCON1 = 0b00000001; //设置扫描通道0-3
OPTION_REG = 0b11010011; //TMR0 分频器1:16
TMR0IF = 0;
TMR0IE = 1;
T1CON = 0b11000101; //计数,时钟来源电容模块1:1分频
T1GCON = 0b00000000; //与门控位无关
TMR1GIF = 0;
TMR1GIE = 0;
TMR1H = 0;
TMR1L = 0;
TMR0 = const_timr0_value;
}
void SetNextSensor(void)
{
if(Index == 3)
{
Index = 0; //Key1-Key4
}
else
{
Index++;
}
CPSCON1 = Index;
}
void RestartTimers(void)
{
TMR1ON = 0;
TMR1H = 0;
TMR1L = 0;
TMR1ON = 1;
}
void Cap_ISR(void)//触摸按键扫描
{
if (Cap_Value < (Cap_Avg[Index] - trip[Index])) //有键按下,不更新频率平均值
{
switch(Index)
{
case 0: //KEY1 ON
{
}
break;
case 1: //KEY2 ON
{
if(ucKeyLock2==0)
{
uiKeyTimeCnt2++; //累加定时中断次数
if(uiKeyTimeCnt2>const_key_time2)
{
uiKeyTimeCnt2 = 0;
ucKeyLock2 = 1;//自锁按键置位,避免一直触发
ucKeySec = 2;//触发2号键
ucKey2TouchCnt++;
}
}
}
break;