gpt4 book ai didi

c - 通过PIC16F877A的UART传输10bit数据

转载 作者:太空宇宙 更新时间:2023-11-04 03:38:23 25 4
gpt4 key购买 nike

我是微 Controller 技术的初学者。我想传输从模数转换获得的 10 位输出,但只能通过 UART 发送 8 位。如何发送 10 位?

请帮我写C代码来解决这个问题。到目前为止,我的代码如下。使用的编译器是XC8。

#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
#define _XTAL_FREQ 4000000

#include <stdio.h>
#include <stdlib.h>

#include <htc.h>

void uart_init(void);
void TX(unsigned char TX_BYTE);
void configure_pins();
unsigned char read_input(unsigned char channel);

void main()
{
__delay_ms(2);
while (1) {
TRISB = 0; //configuring portB as output
TRISC = 0;
TRISA = 1;

configure_pins(); //calling methods
unsigned char x = read_input(0);
uart_initialize();
assign_data_to_tx_pin(x);
}
}

void configure_pins(){
ADCON1 = 0b10000000; //The result is right justified
}

unsigned char read_input(unsigned char channel){ // converting the Analog input to digital
ADCON0=0b00000000;
CHS0=0; // AN0 is selected
CHS1=0; // "
CHS2=0; // "

ADON = 1;
GO_DONE = 1;

while (GO_DONE);
ADON = 0;
return ((ADRESH >> 2) + ADRESL); // return the result of conversion
}

void uart_initialize(void) // initializing the UART for data transmission
{

TRISC = 0; //configuring portC as output

TXSTA = 0b100000000;
TXEN = 1; //enable transmission mode
SPEN = 1; //enable UART
BRGH = 0; //enable low baud
SPBRG = 6; //set baud rate as 9600
SYNC = 0; //enable asynchronous transmission

RCIE = 1;
GIE = 1;
PEIE = 1;
}

void assign_data_to_tx_pin(unsigned char converted_data) { // assigning the data to the Tx pin for transmission
while(!TRMT) {
unsigned char a = converted_data;

TXREG = a;
TXREG = a >> 2;
PORTCbits.RC6 = TXREG;

__delay_ms(100); // Delay
}
}

最佳答案

典型的 UART 不允许每次传输超过 8 位的数据。有些允许 9。在使用 9 位并控制奇偶校验的选定 UART 上可能可以发送 10 位,但这种情况很少见。

相反,建议将数据作为 2 次传输发送,并留出一个位来表示发送的是哪一半。

Send_ADC(void){
ADCON0=0b00000000;
CHS0=0; // AN0 is selected
CHS1=0; // "
CHS2=0; // "
ADON = 1;
GO_DONE = 1;
while (GO_DONE);
ADON = 0;
unsigned adc10 = ((ADRESH >> 2) + ADRESL);
assign_data_to_tx_pin((adc10 % 32) * 2 + 0); // 00lllll0
assign_data_to_tx_pin((adc10 / 32) * 2 + 1); // 00hhhhh1
}

在接收端,确保接收到的字节是正确的字节顺序。这将以正确的顺序重新构造接收到的数据,即使接收没有同步开始或者如果一个字节在通信中丢失。

// return 0: success, else 1
int ReadSerialADC(unsigned *data) {
unsigned adc;
unsigned low = read_from_comport();
if (low %2) return 1;
low /= 2;
unsigned high = read_from_comport();
if (high %2 == 0) return 1;
high /= 2;
*data = high * 32 + low;
return 0;
}

关于c - 通过PIC16F877A的UART传输10bit数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30465558/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com