#申请原创# @21小跑堂
【1】ADC读取设备温度测试:
首先,使用在MCC工具中添加Drivers → ADCC → ADCC,FVR,MEMORY,TMR2,其中先设置固定参考电压FVR,设置如下,稍后该电压将作为ADC的参考电压;
接下来设置TMR2,设置TMR2的周期为0.5s,稍后将使用TMR2来触发ADC进行转换;
接下来设置ADC,使用Burst_average_mode,时钟选择Frc,右对齐,参考电压使用FVR,自动转换触发选择TMR2,由于设置的TMR2周期为0.5s,也就是1秒进行两次AD转换;
最后,使能MEMORY,因为要从中读取参数值。
设置完成后生成程序代码,同时在主函数中增加ADC读取与温度转换程序,编写完成后编译下载程序至开发板。
#include "mcc_generated_files/mcc.h"
#include "stdio.h"
//Sets sampling channel of ADCC without starting conversion
#define SetAcquisitionChannel(X) do { ADPCH = X; } while (0)
void Int0_ISR(void)
{
printf("Key Pressed!\r\n");
}
/*
Main application
*/
void main(void)
{
// Initialize the device
SYSTEM_Initialize();
int16_t gain;
int16_t offset;
gain = FLASH_ReadWord(DIA_TSHR1);
offset = FLASH_ReadWord(DIA_TSHR3);
INTERRUPT_GlobalInterruptEnable();
uint16_t ADC_MEAS = 0;
int24_t temp_c = 0;
SetAcquisitionChannel(channel_Temp);
// If using interrupts in PIC18 High/Low Priority Mode you need to enable the Global High and Low Interrupts
// If using interrupts in PIC Mid-Range Compatibility Mode you need to enable the Global Interrupts
// Use the following macros to:
// Enable the Global Interrupts
INTERRUPT_GlobalInterruptEnable();
// Disable the Global Interrupts
//INTERRUPT_GlobalInterruptDisable();
IO_SW0_SetPullup();
INT0_SetInterruptHandler(Int0_ISR);
while (1)
{
// Add your application code
printf("Hello World!\r\n");
__delay_ms(1000);
asm ("SLEEP");
asm ("NOP");
ADC_MEAS = ADCC_GetConversionResult();
temp_c = (int24_t) (ADC_MEAS) * gain;
temp_c = temp_c / 256;
temp_c = temp_c + offset;
temp_c = temp_c / 10;
printf("Device Temperature: %dC \r\n", temp_c);
}
}
查看串口输出内容:
【2】PWM驱动舵机实验:
舵机是目前小型机器人或云台摄像头等智能产品中常用的一种电机装置,通常其最大旋转角度为180,有的也可以为360°。舵机的控制信号线有三种,分别是+5V、GND和信号线。控制方式采用PWM控制。本次使用单片机的PWM模块驱动SG90舵机,硬件连线如下:
+5V----------------------------------VTG
GND---------------------------------GND
信号线-------------------------------RC7
首先使用MCC配置工具添加Drivers → PWM→ PWM1_16BIT,然后配置PWM模块的时钟,并设置PWM的频率为50Hz,同时将PMW11的输出引脚映射到RC7引脚;
接下来生成程序代码,并在程序中添加控制程序,主要用来改变PWM的占空比,进而控制舵机进行180°转动,程序如下:
#include "mcc_generated_files/mcc.h"
#include "stdio.h"
//Sets sampling channel of ADCC without starting conversion
#define SetAcquisitionChannel(X) do { ADPCH = X; } while (0)
void Int0_ISR(void)
{
printf("Key Pressed!\r\n");
}
/*
Main application
*/
void main(void)
{
// Initialize the device
SYSTEM_Initialize();
int16_t gain;
int16_t offset;
uint16_t dutyVal = 300;
uint8_t dir = 1;
gain = FLASH_ReadWord(DIA_TSHR1);
offset = FLASH_ReadWord(DIA_TSHR3);
INTERRUPT_GlobalInterruptEnable();
uint16_t ADC_MEAS = 0;
int24_t temp_c = 0;
SetAcquisitionChannel(channel_Temp);
// If using interrupts in PIC18 High/Low Priority Mode you need to enable the Global High and Low Interrupts
// If using interrupts in PIC Mid-Range Compatibility Mode you need to enable the Global Interrupts
// Use the following macros to:
// Enable the Global Interrupts
INTERRUPT_GlobalInterruptEnable();
// Disable the Global Interrupts
//INTERRUPT_GlobalInterruptDisable();
IO_SW0_SetPullup();
INT0_SetInterruptHandler(Int0_ISR);
while (1)
{
// Add your application code
if(dir == 1) dutyVal++;
else dutyVal--;
__delay_ms(5);
if(dutyVal <= 100)
dir = 1;
if(dutyVal >= 500)
dir = 0;
PWM1_16BIT_SetSlice1Output1DutyCycleRegister(dutyVal);
PWM1_16BIT_LoadBufferRegisters();
}
}
编写程序完成后,编译程序并下载至开发板,可以看到舵机可以进行相应的运动了。
|