| ??? 11/11/04 10:00 Read: times |
#81026 - UART 9-Bits Mode Operation |
Dear all,
Here is a question about UART serial mode 3, 9-bit and variable baud rate, on a pair of ADuC814. For the best efficient serial communication, I wrote a program that works as follow. ADC routine sends 2 data with 9th bit by 3 bytes for 1 cycle. DAC routine receives these data and make some DA conversion in accordance with the 9th bits by transmittion part and flags by reception part. Because of that this chip has 12-bit resolution, I thought this method is the best to serially communicate. The programs recognize which is which by 9th bits and flags. +--------+--------+--------+--------+--------+--------+--------+--------+ |9th bit | 1 | 0 | 0 | 1 | 0 | 0 | | |data bit|HHHHLLLL|LLLLHHHH|LLLLLLLL|HHHHLLLL|LLLLHHHH|LLLLLLLL| repeat | |flag | 0 | 1 | 0 | 0 | 1 | 0 | | +--------+--------+--------+--------+--------+--------+--------+--------+ From ADuC814 data sheet, TB8 and TR8 should be used for 9th bit operation. I read carefully about this and wrote a program, but things are not going well. AD routine put out serial data correctly, DA routine doesn't put DA result well. I think probably *9th bits operation have problems* on some parts of beyond my understanding. I understand the fact that the following things are a basic method how to operate TB8, RB8, and SBUF, and it accurately is written in the routines. For transmitting: MOV TB8, C ; Copy it into the TB8 bit MOV SBUF, A ; Start transmission by writing to SBUF For receiving: MOV A, SBUF ; Get the received byte MOV C, RB8 ; Get the received parity On the source code below, which part are wrongly used and would you please inform me how to modify... and some correct way of using that. Thank you very much for gracious help. Hiroaki
-----------------------------------------------------FOR TRANSMITTION
#include <stdio.h>
#include <ADuC814.h>
////////// INITIALIZATION //////////
void init(void) {
/* Serial Setting */
PLLCON = 0x00; // 16.777216MHz core clock
SCON = 0xD2; // 9-bit, no_parity, 1-stop_bit
TI = 1; // ready to transmit
/* Baud Rate Setting */
PCON = 0x80; // double baud rate
T2CON = 0x34; // timer2 baud rate generation mode
RCAP2H = 0xFF; // for 57,600bps
RCAP2L = 0xF7; // for 57,600bps
TH2 = 0xFF; // timer2 high byte
TL2 = 0xF7; // timer2 low byte
/* Configure ADC */
ADCCON1 = 0x90; // 0x92 for 246.7KHz sampling
ADCCON2 = 0x40; // ADCSPI
ADCDATAH = 0x00; // ADC high byte
ADCDATAL = 0x00; // ADC low byte
}
////////// ADC ROUTINE //////////
void adc(void) {
unsigned char tmp;
/* start AD conversion */
SCONV = 1; // start ADC
while(SCONV); // wait for ADC completion
/* transmit HHHHLLLL 0 */
while(!TI);
TI = 0;
TB8 = 1; // 9th bit = 1
SBUF = ((ADCDATAH&0x0F) << 4) | ((ADCDATAL&0xF0) >> 4);
/* store bottom 4 bit to tmp and start the next conversion */
tmp = (ADCDATAL&0x0F) << 4; // tmp = LLLLXXXX
SCONV = 1; // start ADC
while(SCONV); // wait for ADC completion
/* transmit LLLLHHHH 1 */
while(!TI);
TI = 0;
TB8 = 0; // 9th bit = 0
SBUF = tmp | (ADCDATAH&0x0F);
/* transmit LLLLLLLL 0 */
while(!TI);
TB8 = 0; // 9th bit = 0
SBUF = ADCDATAL;
}
////////// MAIN PROGRAM //////////
void main(void) {
init();
while(1) {
adc();
}
}
--------------------------------------------------------FOR RECEPTION
#include <stdio.h>
#include <ADuC814.h>
unsigned char a, high, low, flag, nine;
////////// INTIALIZATION //////////
void initialize(void) {
/* Serial Setting */
ADCCON1 = 0x80; // turn on the internal reference
ADCCON2 = 0x09; // select DAC0 channel
DACCON = 0x0D; // configure DAC
DAC0H = 0x00; // DAC high byte
DAC0L = 0x00; // DAC low byte
SCON = 0xD2; // 9-bit, no_parity, 1-stop_bit
PLLCON = 0x00; // 16.777216MHz core clock
/* Baud Rate Setting */
PCON = 0x80; // double baud rate
T2CON = 0x34; // timer2 baud rate generation mode
RCAP2H = 0xFF; // for 57,600bps
RCAP2L = 0xF7; // for 57,600bps
TH2 = 0xFF; // timer2 high byte
TL2 = 0xF7; // timer2 low byte
high = 0;
low = 0;
}
////////// DAC ROUTINE //////////
void dac(void) {
if(RI) {
a = SBUF; // read from UART
nine = RB8;
RI = 0;
if(nine) { // receive HHHHLLLL
high |= (a&0xF0) >> 4; // high: 0000HHHH
low |= (a&0x0F) << 4; // low: LLLLXXXX
flag = 1;
}
else if(flag) { // receive LLLLHHHH
low = (a&0xF0) >> 4; // low = LLLLLLLL
DAC0H = high; // output high 4bits
DAC0L = low; // output low 8bits
high |= a&0x0F; // high: 0000HHHH
flag = 0;
}
else { // receive LLLLLLLL
low |= a; // low: LLLLLLLL
DAC0H = 0x0A; // output high 4bits
DAC0L = 0xAA; // output low 8bits
}
}
}
////////// MAIN PROGRAM //////////
void main(void) {
initialize();
while(1) {
dac();
}
}
|
| Topic | Author | Date |
| UART 9-Bits Mode Operation | 01/01/70 00:00 | |
| RE: UART 9-Bits Mode Operation | 01/01/70 00:00 | |
| RE: UART 9-Bits Mode Operation | 01/01/70 00:00 | |
| RE: UART 9-Bits Mode Operation | 01/01/70 00:00 | |
| RE: UART 9-Bits Mode Operation | 01/01/70 00:00 | |
| RE: UART 9-Bits Mode Operation | 01/01/70 00:00 | |
| RE: UART 9-Bits Mode Operation | 01/01/70 00:00 | |
| RE: UART 9-Bits Mode Operation | 01/01/70 00:00 | |
| RE: UART 9-Bits Mode Operation | 01/01/70 00:00 | |
| RE: UART 9-Bits Mode Operation | 01/01/70 00:00 | |
| RE: UART 9-Bits Mode Operation | 01/01/70 00:00 | |
RE: SPI vs UART 9-Bits Mode | 01/01/70 00:00 |



