Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Read ADC data from ATMega8535 and transfer it using Serial

Status
Not open for further replies.

Rhizudin

Newbie level 4
Newbie level 4
Joined
Jun 26, 2013
Messages
7
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,343
Hi! I have a project to read adc data from atmega8535 microcontroller to PC using serial. I use Codevision AVR for programming application.
I have try this code, but it just shown an unknown character like this -> ÿ

Code:
#include <mega8535.h>
#include <stdio.h>
#include <delay.h>
#define mode_ADC 0x20
const long int osilator=4000000;
unsigned long int UBRR;
unsigned char dataadc;

//prototype function
unsigned char baca_adc (unsigned char pin_adc)
{
  ADMUX=pin_adc |mode_ADC;
  ADCSRA|=0x40;
  while ((ADCSRA & 0x10)==0);
  ADCSRA|=0x10;
  return ADCH;
}
void InisialisasiUART( unsigned long int baud_rate );
void tampilan (void);
void ngurusinADC (void);
void main ( void )
{
  DDRC=0xFF;
  PORTC=0x00;              
  ngurusinADC();
  InisialisasiUART(9600);
  while (1)
  {
  tampilan();     
  dataadc=baca_adc (1);
  delay_ms(1000);
  };
}
void ngurusinADC (void)
{
 //Inisialisasi ADC
  DDRC=0xFF;
  ADMUX=mode_ADC;
  ADCSRA=0x86;   
}
void InisialisasiUART ( unsigned long int baud_rate )
{
  UBRR = (osilator/(16*baud_rate))-1;
  UBRRL = UBRR;
  UBRRH = UBRR>>8;
  UCSRA = 0x00;
  UCSRB = 0x18;
  UCSRC = 0x86;
}
void tampilan (void)
{
  putchar(dataadc);
}

After that, i try to change this code :
Code:
 putchar(dataadc);
to
Code:
puts(dataadc);
But there is an error message " function argument #1 of type 'unsigned char' is incompatible with required parameter of type 'unsigned char *' "

So, whats wrong with my code? anyone know please ?
 

1st try to send single character to hyperterminal and be confirm about communication between PC and uC board.

Make sure you are fusing low and high fuse bytes according to your crystal oscilator..

1st try to send char and then try to send string . . and then use ADC and make your program according to it..
 

Try this code, it's work just fine.

See attached file, you can find complete project and ISIS Proteus simulation.
You may recompile this project using CVAVR 2.04.4a or later.

By default it will send string decimal data instead of character, you can modify the data formatting in the printf() function

Happy Programming :)

Herlambang,

Code:
;Compiler   : CodevisionAVR V2.04.4a
;CHIP        : AVR ATMega16 @4Mhz
;Prog. Exp  : ADC UART example..
-------

*/

#include <mega16.h>

// Standard Input/Output functions
#include <delay.h>     //library fungsi delay
#include <stdio.h>     //I/O funtion

#define ADC_VREF_TYPE 0x40  //AVCC reference

// Global variables
const long int osilator=4000000;
unsigned long int UBRR;

//function prototype
void sett_regs(void);
void SetBaudrate( unsigned long int baud_rate );


// Read the AD conversion result
unsigned int read_adc(unsigned char adc_input){
ADMUX=adc_input | (ADC_VREF_TYPE & 0xff);
// Delay needed for the stabilization of the ADC input voltage
delay_us(10);
// Start the AD conversion
ADCSRA|=0x40;
// Wait for the AD conversion to complete
while ((ADCSRA & 0x10)==0);
ADCSRA|=0x10;
return ADCW;
}

//Main routine buat super loop
void main(void){
sett_regs();  
SetBaudrate(9600);   
for(;;){ 
    #asm("nop")
    //utilize printf function from stdio.h. sometimes will needs CRLF to monitor the data in terminal software
    //CRLF has the function to make a new line with every packet sent
    //printf("Data ADC = %d%c%c", read_adc(0),13,10); //printf dengan carriage return and linefeed (13,10)  CRLF
    //printf("Data ADC = %d\r\n", read_adc(0));       //printf dengan carriage return and linefeed (13,10)  CRLF
    printf("Data ADC = %d", read_adc(0)); //printf tanpa carriage return and linefeed (13,10)  CRLF  
    //carriage return dan linefeed terpisah :)
    putchar(13);  //carriage return
    putchar(10);  //linefeed
    delay_ms(500); //delay for packet, time interval                                         
    }
}

// Setting register
void sett_regs(void){
//Define I/O 
DDRA  = 0x00;PORTA = 0x00;
DDRB  = 0x00;PORTB = 0x00;
DDRC  = 0x00;PORTC = 0x00;
DDRD  = 0xF0;PORTD = 0xF0;
// ADC initialization
// ADC Clock frequency: 31,250 kHz
// ADC Voltage Reference: AVCC pin
// ADC Auto Trigger Source: Free Running
ADMUX=ADC_VREF_TYPE & 0xff;
ADCSRA=0xA7;
SFIOR&=0x1F;
}                    

void SetBaudrate( unsigned long int baud_rate ){
UBRR = (osilator/(16*baud_rate))-1;
UBRRL = UBRR;
UBRRH = UBRR>>8;
UCSRA = 0x00;
UCSRB = 0x18;
UCSRC = 0x86;
}
 

Attachments

  • SerialADC_Comm.rar
    83.8 KB · Views: 91
Last edited by a moderator:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top