PIC18F45K80 USART

2019-04-15 12:23发布

初期参考了三篇文章 1、PIC18F45K80单片机串口调试总结
http://blog.csdn.net/sandyde123/article/details/48091535

2、用示波器 查看波特率是多少?
http://blog.csdn.net/androidolblog/article/details/47659149

3、关于示波器测485串口波特率的使用方法总结
http://blog.csdn.net/lianxue1986/article/details/50385384
根据手册



/* * File: Main.c * Author: Alex * 单片机型号:PIC18F45k80 -I/PT * IDE:MPLAB X * 编译器:HI-TECH PICC18 * 晶振:16MHz * 引脚:TX1 RC6 * Created on 2017年2月28日, 下午2:34 */ // PIC18F45K80 Configuration Bit Settings // 'C' source line config statements #include #include //配置位 #pragma config CONFIG1L = 0x1D //__CONFIG(1, RETEN_OFF & INTOSCSEL_HIGH & SOSCSEL_HIGH & XINST_OFF); //因为我用的是外部时钟源,16MHz,配置为HS2 //0011 = HS1, HS 振荡器(中等功耗, 4 MHz-16 MHz) //0010 = HS2, HS 振荡器(高功耗, 16 MHz-25 MHz) #pragma config CONFIG1H = 0x2 //__CONFIG(2, FOSC_HS2 & PLLCFG_OFF & FCMEN_OFF & IESO_OFF); #pragma config CONFIG2L = 0x7F //__CONFIG(3, PWRTEN_OFF & BOREN_SBORDIS & BORV_3 & BORPWR_ZPBORMV); #pragma config CONFIG2H = 0x7F //__CONFIG(4, WDTEN_SWDTDIS & WDTPS_1048576); #pragma config CONFIG3H = 0x89 //__CONFIG(5, CANMX_PORTB & MSSPMSK_MSK7 & MCLRE_ON); #pragma config CONFIG4L = 0x91 //__CONFIG(6, STVREN_ON & BBSIZ_BB2K); #pragma config CONFIG5L = 0xF //__CONFIG(7, CP0_OFF & CP1_OFF & CP2_OFF & CP3_OFF); #pragma config CONFIG5H = 0xC0 //__CONFIG(8, CPB_OFF & CPD_OFF); #pragma config CONFIG6L = 0xF //__CONFIG(9, WRT0_OFF & WRT1_OFF & WRT2_OFF & WRT3_OFF); #pragma config CONFIG6H = 0xE0 //__CONFIG(10, WRTC_OFF & WRTB_OFF & WRTD_OFF); #pragma config CONFIG7L = 0xF //__CONFIG(11, EBTR0_OFF & EBTR1_OFF & EBTR2_OFF & EBTR3_OFF); #pragma config CONFIG7H = 0x40 //__CONFIG(12, EBTRB_OFF); /* * */ int main(int argc, char** argv) { /*串口初始化*/ SPBRGH1=0; SPBRG1 = 103; //9600 TXSTA1bits.BRGH=0; BAUDCON1bits.BRG16 = 1; //??8?????? TXSTA1bits.SYNC=0; RCSTA1bits.SPEN=1; TXSTA1bits.TXEN=1; /*串口初始化结束*/ while(1) { TXREG1='V'; while(PIR1bits.TXIF == 0); TXREG1='='; while(PIR1bits.TXIF == 0); } }
XINST:扩展指令集使能位
1 = 使能指令集扩展和变址寻址模式
0 = 禁止指令集扩展和变址寻址模式(传统模式) #pragma config CONFIG1L = 0x1D //__CONFIG(1, RETEN_OFF & INTOSCSEL_HIGH & SOSCSEL_HIGH & XINST_OFF);这里是够开启扩展指令集,输出结果是有差别的,在这里栽过跟头。


Project中需要两个头文件,需添加。

波特率9600

104.0us