本帖最后由 masterqqq 于 2020-8-12 10:15 编辑
调试环境:MPLAB x IDE5.40 + XC8
项目属性的PKOB 通讯速率需要设置位0.75M才能正常下载
USART输出测试
#define F_CPU (4000000UL) /* using default clock 4MHz*/
#define USART1_BAUD_RATE(BAUD_RATE) ((float)(64 * 4000000 / (16 * (float)BAUD_RATE)) +0.5)
#include <avr/io.h>
#include <util/delay.h>
#include <string.h>
void USART1_init(void);
void USART1_sendChar(char c);
void USART1_sendString(char str[]);
void USART1_init(void)
{
PORTC.DIRSET = PIN0_bm; /* set pin 0 of PORT C (TXd) as output*/
PORTC.DIRCLR = PIN1_bm; /* set pin 1 of PORT C (RXd) as input*/
USART1.BAUD = (uint16_t)USART1_BAUD_RATE(9600); /* set the baud rate*/
USART1.CTRLC = USART_CHSIZE0_bm
| USART_CHSIZE1_bm; /* set the data format to 8-bit*/
USART1.CTRLB |= USART_TXEN_bm; /* enable transmitter*/
}
void USART1_sendChar(char c)
{
while(!(USART1.STATUS & USART_DREIF_bm))
{
;
}
USART1.TXDATAL = c;
}
void USART1_sendString(char str[])
{
for(int i = 0; i < strlen(str); i++)
{
USART1_sendChar(str[i]);
}
}
int main(void)
{
char str[40] ="Curiosity Nano USART ";
USART1_init();
while (1)
{
USART1_sendString(str);
_delay_ms(1000);
}
}
|