我是编程Pic-microcontroller的新手。我正在尝试对图片进行编程,以使用MPLAB与HC-05蓝牙设备进行通信。我使用Arduino开发板对其进行了原始编程,并且可以运行,但是当我尝试转换为图片时,它不起作用。我不确定UART和中断的代码部分是否正确。当我运行程序时,电机一直处于开机状态。仅当蓝牙与设备进行通讯以告知其打开时才打开。
// CONFIG1
#pragma config FEXTOSC = OFF // External Oscillator mode selection bits (HS (crystal oscillator) above 4MHz; PFM set to high power)
#pragma config RSTOSC = EXT1X // Power-up default value for COSC bits (HFINTOSC (1MHz))
#pragma config CLKOUTEN = OFF // Clock Out Enable bit (CLKOUT function is disabled; i/o or oscillator function on OSC2)
#pragma config CSWEN = ON // Clock Switch Enable bit (Writing to NOSC and NDIV is allowed)
#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable bit (FSCM timer enabled)
// CONFIG2
#pragma config MCLRE = ON // Master Clear Enable bit (MCLR pin is Master Clear function)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config LPBOREN = OFF // Low-Power BOR enable bit (ULPBOR disabled)
#pragma config BOREN = OFF // Brown-out reset enable bits (Brown-out Reset Enabled, SBOREN bit is ignored)
#pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (VBOR) set to 1.9V on LF, and 2.45V on F Devices)
#pragma config ZCD = OFF // Zero-cross detect disable (Zero-cross detect circuit is disabled at POR.)
#pragma config PPS1WAY = ON // Peripheral Pin Select one-way control (The PPSLOCK bit can be cleared and set only once in software)
#pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable bit (Stack Overflow or Underflow will cause a reset)
// CONFIG3
#pragma config WDTCPS = WDTCPS_31// WDT Period Select bits (Divider ratio 1:65536; software control of WDTPS)
#pragma config WDTE = OFF // WDT operating mode (WDT Disabled, SWDTEN is ignored)
#pragma config WDTCWS = WDTCWS_7// WDT Window Select bits (window always open (100%); software control; keyed access not required)
#pragma config WDTCCS = SC // WDT input clock selector (Software Control)
// CONFIG4
#pragma config BBSIZE = BB512 // Boot Block Size Selection bits (512 words boot block size)
#pragma config BBEN = OFF // Boot Block Enable bit (Boot Block disabled)
#pragma config SAFEN = OFF // SAF Enable bit (SAF disabled)
#pragma config WRTAPP = OFF // Application Block Write Protection bit (Application Block not write protected)
#pragma config WRTB = OFF // Boot Block Write Protection bit (Boot Block not write protected)
#pragma config WRTC = OFF // Configuration Register Write Protection bit (Configuration Register not write protected)
#pragma config WRTSAF = OFF // Storage Area Flash Write Protection bit (SAF not write protected)
#pragma config LVP = OFF // Low Voltage Programming Enable bit (High Voltage on MCLR/Vpp must be used for programming)
// CONFIG5
#pragma config CP = OFF // UserNVM Program memory code protection bit (UserNVM code protection disabled)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
#include <stdint.h>
#define _XTAL_FREQ 12000000
#define Baud 115200
void UART_RX_Init(void);
uint32_t UART_Buffer = 0;
unsigned char Suit = 0x00;
unsigned char Value = 0x00;
int counter = 0;
unsigned char t;
int quotient = 0;
int remainder = 0;
int i= 0;
int j=0;
void main(void)
{
UART_RX_Init();
ANSELA=0;
ANSELC=0;
LATAbits.LATA2 = 0;
t = 0x00;
while(1)
{
while (UART_Buffer == 0){}
while (UART_Buffer != 0)
{
t = UART_Buffer;
UART_Buffer = 0;
}
if (UART_Buffer == 0)
{
if (t == 0x58)
{
counter = 0;
}
else if (t == 0x59)
{
counter = 0;
}
else if (t == 0x5A)
{
}
else{
counter++;
Suit = t;
}
if (counter == 0){
for (int i = 0; i < 7; i++){
LATAbits.LATA2 = 1;
__delay_ms(200);
LATAbits.LATA2 = 0;
__delay_ms(200);
}
}
}
else if (counter > 0){
counter = 0;
Value = t;
quotient = Suit/0x04;
remainder = Suit%0x04;
for (i = 0; i < quotient; i++){
LATAbits.LATA2 = 1;
__delay_ms(500);
LATAbits.LATA2 = 0;
__delay_ms(500);
}
for ( i = 0; i < remainder; i++){
LATAbits.LATA2 = 1;
__delay_ms(250);
LATAbits.LATA2 = 0;
__delay_ms(250);
}
__delay_ms(1000);
quotient = Value/0x04;
remainder = Value%0x04;
for ( j = 0; j < quotient; j++){
LATAbits.LATA2 = 1;
__delay_ms(500);
LATAbits.LATA2 = 0;
__delay_ms(500);
}
for ( j = 0; j < remainder; j++){
LATAbits.LATA2 = 1;
__delay_ms(250);
LATAbits.LATA2 = 0;
__delay_ms(250);
}
}
}
}
void UART_RX_Init()
{
SP1BRGH = 1;
SP1BRGL = 5;
TX1STAbits.SYNC = 0;
RCSTAbits.SPEN = 1;
TRISC5 = 1;
TRISC4 = 1;
RC1IE = 1;
PEIE = 1;
GIE = 1;
RC1STAbits.CREN = 1;
}
void __interrupt() my_isr(void)
{
if (CSWIF == 1)
{
UART_Buffer = RCREG;
CSWIF = 0;
}
}