Hi again.
I'm starting this new thread to ask questions about Mega4808 USART device.
I followed the tutorial for this device posted here.
I have the code divided by 2 files because I'm trying to use ADC device and show values via USART device and see them in my terminal window.
So, in onde file I have the ADC code and main() funciton:
/* * File: ADC_SingleConv.c * Author: narayan * * Created on 27 de Fevereiro de 2019, 22:45 */ #define F_CPU 20000000UL #include <stdio.h> #include <stdlib.h> #include <avr/io.h> #include <inttypes.h> #include <util/delay.h> #include "usart.h" #define ADC_SHIFT_DIV64 (6) /*Divide by 64*/ void CPU_Clk_Init(void){ // Enable Protected Registers Write and select Internal Clock Oscillator of 20MHz and enable prescaler CPU_CCP = CCP_IOREG_gc; CLKCTRL_MCLKCTRLA = CLKCTRL_CLKSEL_OSC20M_gc; // Enable Protected Registers Write and set CPU clock prescaler CPU_CCP = CCP_IOREG_gc; CLKCTRL_MCLKCTRLB = CLKCTRL_PDIV_16X_gc | CLKCTRL_PEN_bm; } void ADC0_Init(void){ // Disable Digital Input Buffer and Pull-Up PORTD_PIN6CTRL &= ~(PORT_ISC_gm | PORT_PULLUPEN_bm); PORTD_PIN6CTRL |= PORT_ISC_INPUT_DISABLE_gc; // Set ADC Prescaler and Reference Voltage ADC0_CTRLC = ADC_PRESC_DIV16_gc | ADC_REFSEL_INTREF_gc; // Set ADC resolution and Enable conversion ADC0_CTRLA = ADC_RESSEL_10BIT_gc | ADC_ENABLE_bm; // Set ADC channel ADC0_MUXPOS = ADC_MUXPOS_AIN5_gc; // Set ADC Mode to Accumulator - 64 values ADC0_CTRLB = ADC_SAMPNUM_ACC64_gc; } uint16_t ADC_Read(void){ // Start selected Conversion ADC0_COMMAND = ADC_STCONV_bm; while( !(ADC0_INTFLAGS & ADC_RESRDY_bm) ) /*wait*/; // Clear RESRDY register before starting new conversion ADC0_INTFLAGS = ADC_RESRDY_bm; return ADC0_RES; } /* * */ int main(int argc, char** argv) { //uint16_t adcRead = 0; CPU_Clk_Init(); ADC0_Init(); USART0_Init(); while(1){ //adcRead = ADC_Read(); //adcRead >>= ADC_SHIFT_DIV64; USART0_SendStr("A\n"); //USART1_SendChar('\n'); _delay_ms(10); } return (EXIT_SUCCESS); }
and in the other file I have the USART code:
#ifndef F_CPU # define F_CPU 20000000 #endif #include <avr/io.h> #include <string.h> #include "usart.h" #define USART0_BAUD_RATE(BAUD_RATE) ((float)((20000000/16) * 64 / (16 * (float)BAUD_RATE)) + 0.5) void USART0_Init(void){ PORTA_DIR |= PIN0_bm; PORTA_DIR &= ~PIN1_bm; USART0_BAUD = (uint16_t) USART0_BAUD_RATE(9600); USART0_CTRLB |= USART_TXEN_bm; } void USART0_SendChar(uint8_t ch){ while( !(USART0_STATUS & USART_DREIF_bm) ) /*wait*/; USART0_TXDATAL = ch; } void USART0_SendStr(char* str){ for(size_t i = 0; i < strlen(str); i++){ USART0_SendChar(str[i]); } }
For now I'm just trying to see if I'm sending the 'A' and the '\n' chars correctly and it looks like it is correct.
This picture is to show correct settings for 9600 baud rate. 1/104us = ~9615Hz
Then the complete transmission captured by my scopes LA.
And this is my interpretation of this pulse train
Going through it:
A is 0x41 or 65 decimal or 0100 0001. USART is LSB first, then MSB, so it should be 1000 0010 excluding start and stop bits.
\n is 0x0a or 10 decimal or 0000 1010. USART is LSB first, then MSB, so it should be 0101 0000 excluding start and stop bits.
The fact is that I can't pick this up with minicom or cutecom! I can't see anything