图 16F913 UART 通讯设置
我在下面附上了我的代码(顺便说一句,我闪烁 LED 以便能够看到循环正在发生)。
我没有通过 UART 发送任何数据,我有图 16F913,引脚 17 是我想要获得的输出,我将它连接到 RF-42N 蓝牙模块,我知道该模块可以工作,因为如果我将接收器连接到发送器,我会收到回声。然后,我尝试以下设置将字母“z”发送到蓝牙模块(并希望使用 blueterm 在我的手机上读取)。 我没有得到任何数据,我不知道默认的 FOSC 设置为多少,所以我尝试了 SPBRG 的各种值,希望我能够“猜测”正确的值,但似乎没有一个能够“猜出”正确的值工作。
我正在使用 Hi-Tech C 编译器(免费版)。
我希望这对每个人来说都是足够的信息,我只是不知道我错过了什么,我已经为此奋斗了几个小时。
// 主文件
#include <uart.h>
__CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT & UNPROTECT & BORDIS & IESODIS & FCMDIS);
int i, j;
void wait(int z);
int main()
{
PORTA = 0x0;
CMCON0 = 7;
// Turn off Comparators
ANSEL = 0;
// Turn off ADC
// Trisa4/5 0's mean output, 1's mean input
TRISA4 = 0;
// Make RA4/RA5 Outputs
TRISA5 = 0;
setupAsyncUart();
while (1==1)
{
RA4 = 0;
send('Z');
wait(100);
RA4 = 1;
wait(100);
}
}
// Wait routine.
void wait(int z)
{
for (int a=0; a<z; a++)
{
for (int b=0; b<z; b++)
{
}
}
}
// uart.h
void send(char string)
{
TXREG = string;
}
void setupAsyncUart(int BAUDRATE)
{
SPBRG = 10;
BRGH = 1; //Low speed = 0 high speed = 1
SYNC = 0;
SPEN = 1;
TXEN = 1;
}
I've attached my code below (I'm blinking a LED to be able to see that the loop is occurring BTW).
I am not getting any data sent out over the UART, I have the Pic 16F913, and pin 17 is the output I'm trying to get, I have it hooked up to a RF-42N bluetooth module, I know the module works, because if I connect the rx to the tx, I get an echo. I then tried the following setup to send the letter "z" to the bluetooth module (and hopefully read on my phone, using blueterm).
I am getting no data, I don't know what the default FOSC is set to, so I've tried various values for the SPBRG hoping that i would be able to "guess" the right one, but none of them have seemed to work.
I'm using the Hi-Tech C compiler (Free version).
I hope this is enough information for everyone, I just have no clue what I am missing, I"ve been fighting this for hours.
// Main file
#include <uart.h>
__CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT & UNPROTECT & BORDIS & IESODIS & FCMDIS);
int i, j;
void wait(int z);
int main()
{
PORTA = 0x0;
CMCON0 = 7;
// Turn off Comparators
ANSEL = 0;
// Turn off ADC
// Trisa4/5 0's mean output, 1's mean input
TRISA4 = 0;
// Make RA4/RA5 Outputs
TRISA5 = 0;
setupAsyncUart();
while (1==1)
{
RA4 = 0;
send('Z');
wait(100);
RA4 = 1;
wait(100);
}
}
// Wait routine.
void wait(int z)
{
for (int a=0; a<z; a++)
{
for (int b=0; b<z; b++)
{
}
}
}
// uart.h
void send(char string)
{
TXREG = string;
}
void setupAsyncUart(int BAUDRATE)
{
SPBRG = 10;
BRGH = 1; //Low speed = 0 high speed = 1
SYNC = 0;
SPEN = 1;
TXEN = 1;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您必须将内部振荡器设置为适当的 CPU 频率。
因此首先设置 __CONFIG 寄存器位 FOSC<2:0> = (100) 选择内部振荡器,然后在编程开始时将 OSCCON 寄存器中的 IRCF2、IRCF1 和 IRCF0 位设置为所需的振荡器频率,默认频率为 4Mhz。
不是初始化 UART 而是
CPU 时钟频率(查看数据表)。
(检查数据表)。
编辑:
要配置内部时钟使用:
您应该在“pic16f91x.h”文件中找到所有其他位声明。
您应该在“cas16f913.h”文件中找到 OSCCON 定义...
First off all you must to set the internal oscillator to appropriate CPU frequency.
So set first in __CONFIG register bits FOSC<2:0> = (100) to select internal oscillator, after that set at very beginning of program the bits IRCF2, IRCF1 and IRCF0 in OSCCON register to desired oscillator frequency, default frequency is 4Mhz.
Than init the UART
CPU clock frequency (check datasheet).
(check datasheet).
EDIT:
To config internal clock use:
You should find all other bit declarations in "pic16f91x.h" file.
OSCCON definition you should find in "cas16f913.h" file...
为了确保您的 TX 函数能够正常工作,请按以下方式使用:
此 while 循环检查发送中断标志(即使未设置)以查看 TXREG 是否为空或仍设置为先前的值。
根据您的 XTAL 频率,您的 SPBRG 值会有所不同,请正确计算它,然后使用计算机检查您是否具有正确的波特率值。
Just to be sure that your TX function will work, use it this way :
This while loop checks the transmit interrupt flag (even if not set) to see if the TXREG is empty or still set with previous value.
Depending on your XTAL frequency, your SPBRG value will vary, calculate it correctly then check with a computer if you have the right baudrate value.