0

我目前正在使用 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)

4

1 回答 1

0

您的“SerialInit()”和“SerialTx('S')”函数在做什么?贴出这些函数的代码,我想看看你是怎么设置你的串口的

可能您还没有设置所有必需的位(RCSTA、TXSTA、SPBRG)。要正确设置这些位,请务必知道您想要的适当波特率,如果您需要高速,请务必正确设置 SPBRGH。

正确初始化后通过UART传输的两个功能:

void vTx232 (UC ucSend)
 {
    STATUS.RP0 = PIR1;     //Sure we're in PIR1
    while (PIR1.TXIF == 0);//While last TX not done
    TXREG = ucSend;        //Input param into TXREG
 }

void vTxString(UC *ucpString)
 {
   while (*ucpString!= 0x00)    //While string not at its end
    {
      vTx232(*ucpString);       //Send string character
      ucpString++;              //Increm string pointer
    }
 }

数据表:PIC12LF1822

于 2014-06-10T20:50:02.803 回答