我目前正在使用 Microchip 系列控制器 PIC12LF1822。因为我需要将 EUSART 配置为 8 位异步模式并使用 PWM 生成 1MHZ 的脉冲......
首先,我从 UART 发送和接收数据开始。但是在配置寄存器中设置包括振荡器选择后,我的代码不起作用并且无法通过 tx 引脚(RA0)发送字符。
我进行了深入分析,但我无法找到解决方案。任何人都可以建议解决这个问题。
下面我粘贴了我的代码,
#include<htc.h>
#include <PIC12LF1822.H>
#include "BIPOLAR_SERIAL.C"
#define _XTAL_FREQ 12000000
//Configuration word 1 register for oscillator selection
__CONFIG(CP_OFF & BOREN_OFF & WDTE_OFF & IESO_OFF & FCMEN_OFF & PWRTE_ON & CPD_OFF & FOSC_XT);
//Configuration word 2 register for disabling Low-Voltage program
__CONFIG(LVP_OFF);
void PWM_Duty_Cyle(unsigned int duty_cyc)
{
CCPR1L = duty_cyc>>2;
CCP1CON &= 0xcf;
CCP1CON |= ((duty_cyc & 0x03)<<4);
}
void PWM_Init()
{
TRISA2 = 0; //PWM Pin as output
CCP1CON = 0x0c; //PWM mode: P1A, P1C active-high; P1B, P1D active-high
PR2 = 0x00; //Timer Period Configuration
T2CON = 0x00; //Timer2 Prescale as 1
PWM_Duty_Cyle(0); //Initialize PWM to 0 percent duty cycle
T2CON |= 0x04; //Enable Timer2
}
void main()
{
APFCON = 0;
ANSA0=0;ANSA1=0;ANSA2=0;
serial_init();
serial_tx('S');
// PWM_Init();
// serial_str("PIC12LF1822_TEST");
while(1)
{
serial_tx('A');
serial_tx(' ');
}
}
在上面的代码中,我无法获得字符的“A”和“S”。
注意:我使用 12MHZ 作为 Fosc,我已经根据它设置了波特率..(SPBRG=77 for baud =900,brgh=1)