[En-Nut-Discussion] Here is code, "I need Help to get data"
saime aley
saime.aley at gmail.com
Wed Mar 15 19:00:45 CET 2006
Dear Members,
I am trying to read the Channel "0" at Analog port...i connected the
temperature sensor with that port,,,
Can anyone help me, because i am getting charater data like "ow" even i
tried to convert it to the long, but it is giving nothing then,,
i am using following code,,,
Even little help will be appericiated in any regard.
Thanx in advance,
With Best Regards,
Saime
#include <cfg/crt.h> /* Floating point configuration. */
#include <string.h>
#include <stdio.h>
#include <io.h>
#include <cfg/arch.h>
#include <dev/board.h>
#include <sys/atom.h>
#include <sys/thread.h>
#include <sys/timer.h>
#ifdef __GNUC__
#include <string.h>
#include <avr/sleep.h>
#include <sys/heap.h>
#include <sys/atom.h>
#include <sys/nutconfig.h>
#include <dev/irqreg.h>
#include <dev/adc.h>
#ifndef ADC_INITIAL_CHANNEL
#define ADC_INITIAL_CHANNEL ADC0
#endif
#ifndef ADC_INITIAL_REF
#define ADC_INITIAL_REF AVCC
#endif
#ifndef ADC_INITIAL_MODE
#define ADC_INITIAL_MODE SINGLE_CONVERSION
#endif
#ifndef ADC_INITIAL_PRESCALE
#define ADC_INITIAL_PRESCALE ADC_PRESCALE_DIV64
#endif
#define ADC_BUF_SIZE 16 // this may only be a power of two
#if defined(__GNUC__) && defined(__AVR_ENHANCED__)
u_char adc_sleep_mode = SLEEP_MODE_ADC;
/* AT90CAN128 uses a different register to enter sleep mode */
#if defined(SMCR)
#define AVR_SLEEP_CTRL_REG SMCR
#else
#define AVR_SLEEP_CTRL_REG MCUCR
#endif
#endif
/********** DRIVER GLOBALS **********/
adc_mode_t current_mode;
// The buffers are FIFO buffers implemented as char
// arrays with head and tail pointers.
// All read and write functions return a 0 for
// success and a 1 for fail, if a successfull read,
// the character is placed into *read.
// buffer: [data1 data2 ... datalast head tail]
#define _adc_buf_head ADC_BUF_SIZE
#define _adc_buf_tail ADC_BUF_SIZE+1
static char *banner = "\n.../JUst nothing\n";
static prog_char presskey_P[] = "it seems working...??";
static prog_char pgm_ptr[] = "\nHi Hi!\n";
u_int adw[8];
u_long baud = 38400;
u_short *ADC_buffer;
inline int ADCBufRead(u_short * buf, u_short * read)
{
u_char tail, head;
tail = buf[_adc_buf_tail];
head = buf[_adc_buf_head];
if (head != tail) {
*read = buf[tail];
buf[_adc_buf_tail] = (tail + 1) & (ADC_BUF_SIZE-1);
return 0;
}
return 1;
}
inline int ADCBufWrite(u_short * buf, u_short * write)
{
u_char tail, head;
tail = buf[_adc_buf_tail];
head = buf[_adc_buf_head];
if ((head + 1) % ADC_BUF_SIZE != tail) {
buf[head] = *write;
buf[_adc_buf_head] = (head + 1) & (ADC_BUF_SIZE-1);
return 0;
}
return 1;
}
void ADCBufInit(u_short * buf)
{
buf[_adc_buf_head] = 0;
buf[_adc_buf_tail] = 0;
}
static void ADCInterrupt(void *arg)
{
u_short ADC_value;
ADC_value = inw(ADCW);
if (ADCBufWrite(ADC_buffer, &ADC_value) != 0) {
// Send error message
}
}
void ADCInit()
{
ADCSetChannel(ADC_INITIAL_CHANNEL);
ADCSetRef(ADC_INITIAL_REF);
ADCSetMode(ADC_INITIAL_MODE);
ADCSetPrescale(ADC_INITIAL_PRESCALE);
ADC_buffer = NutHeapAlloc(sizeof(u_short) * ADC_BUF_SIZE + 2);
ADCBufInit(ADC_buffer);
if (NutRegisterIrqHandler(&sig_ADC, ADCInterrupt, NULL)) {
// NutExitCritical();
return;
}
// Enable ADC
sbi(ADCSR, ADEN);
// Enable ADC interrupt
sbi(ADCSR, ADIE);
}
void ADCSetRef(adc_ref_t reference)
{
ADCStopConversion();
#ifdef __AVR_ENHANCED__
switch (reference) {
case AVCC:
cbi(ADMUX, REFS1);
sbi(ADMUX, REFS0);
break;
case AREF:
cbi(ADMUX, REFS1);
cbi(ADMUX, REFS0);
break;
case INTERNAL_256:
sbi(ADMUX, REFS1);
sbi(ADMUX, REFS0);
break;
}
#endif /* __AVR_ENHANCED__ */
}
void ADCSetMode(adc_mode_t mode)
{
ADCStopConversion();
switch (mode) {
case FREE_RUNNING:
sbi(ADCSR, ADFR);
current_mode = mode;
break;
case SINGLE_CONVERSION:
cbi(ADCSR, ADFR);
current_mode = mode;
break;
}
}
u_char ADCSetPrescale(u_char prescalar)
{
ADCStopConversion();
if (prescalar > 128) {
prescalar = 128;
}
switch (prescalar) {
case ADC_PRESCALE_DIV2:
cbi(ADCSR, ADPS2);
cbi(ADCSR, ADPS1);
cbi(ADCSR, ADPS0);
break;
case ADC_PRESCALE_DIV4:
cbi(ADCSR, ADPS2);
sbi(ADCSR, ADPS1);
cbi(ADCSR, ADPS0);
break;
case ADC_PRESCALE_DIV8:
cbi(ADCSR, ADPS2);
sbi(ADCSR, ADPS1);
sbi(ADCSR, ADPS0);
break;
case ADC_PRESCALE_DIV16:
sbi(ADCSR, ADPS2);
cbi(ADCSR, ADPS1);
cbi(ADCSR, ADPS0);
break;
case ADC_PRESCALE_DIV32:
sbi(ADCSR, ADPS2);
cbi(ADCSR, ADPS1);
sbi(ADCSR, ADPS0);
break;
case ADC_PRESCALE_DIV64:
sbi(ADCSR, ADPS2);
sbi(ADCSR, ADPS1);
cbi(ADCSR, ADPS0);
break;
case ADC_PRESCALE_DIV128:
sbi(ADCSR, ADPS2);
sbi(ADCSR, ADPS1);
sbi(ADCSR, ADPS0);
break;
default:
return 1;
break;
}
return 0;
}
void ADCSetChannel(adc_channel_t adc_channel)
{
u_char current_admux;
current_admux = inb(ADMUX) & 0xF8;
outb(ADMUX, (current_admux | adc_channel));
}
void ADCBufferFlush(void)
{
ADCBufInit(ADC_buffer);
}
void ADCStartConversion()
{
sbi(ADCSR, ADSC);
}
void ADCStartLowNoiseConversion()
{
ADCSetMode(SINGLE_CONVERSION);
#if defined(__GNUC__) && defined(__AVR_ENHANCED__)
{
u_char sleep_mode = AVR_SLEEP_CTRL_REG & _SLEEP_MODE_MASK;
set_sleep_mode(adc_sleep_mode);
/* Note: avr-libc has a sleep_mode() function, but it's broken for
AT90CAN128 with avr-libc version earlier than 1.2 */
AVR_SLEEP_CTRL_REG |= _BV(SE);
__asm__ __volatile__ ("sleep" "\n\t" :: );
AVR_SLEEP_CTRL_REG &= ~_BV(SE);
set_sleep_mode(sleep_mode);
}
#else
sbi(ADCSR, ADSC);
#endif
}
void ADCStopConversion()
{
if (current_mode == SINGLE_CONVERSION) {
// Send warning message
return;
}
// Terminate and restart the ADC
// When restarted, start_conversion needs to be
// called again
cbi(ADCSR, ADEN);
cbi(ADCSR, ADSC);
sbi(ADCSR, ADEN);
}
u_char ADCRead(u_short * value)
{
return ADCBufRead(ADC_buffer, value);
}
inline adc_mode_t ADCGetMode(void)
{
return (current_mode);
}
#endif
/*@}*/
int main(void)
{
int got;
int l;
char *cp;
long display_data;
u_short data1[8];
FILE *uart;
#ifdef STDIO_FLOATING_POINT
float dval = 0.0;
#endif
/*Registering UART Device */
NutRegisterDevice(&DEV_UART, 0, 0);
/*Setting it as output device*/
uart = fopen(DEV_UART_NAME, "r+");
_ioctl(_fileno(uart), UART_SETSPEED, &baud);
_write(_fileno(uart), banner, strlen(banner));
{
_write_P(_fileno(uart), presskey_P, sizeof(presskey_P));
}
_write(_fileno(uart), 0, 0);
fputs("\nHere We Go!!!!!!! ", uart);
fputs("Are you Sure it is working?",uart);
ADCInit();
cp= ADCRead(data1);
fputs("Data Will be Displayed Here\n\n",uart);
_write_P(_fileno(uart), cp, sizeof(cp));
display_data= atol( cp );
fputs("\nLong Data Will be Displayed Here\n\n",uart);
_write_P(_fileno(uart), display_data, sizeof(display_data));
fflush(uart);
}
More information about the En-Nut-Discussion
mailing list