[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: RE: RE: [avr-gcc-list] buggy variable naming with underscores
From: |
Jamie Morken |
Subject: |
Re: RE: RE: [avr-gcc-list] buggy variable naming with underscores |
Date: |
Tue, 15 Mar 2005 08:08:20 -0800 |
Hi,
----- Original Message -----
From: Ben Mann <address@hidden>
Date: Monday, March 14, 2005 5:22 pm
Subject: RE: RE: [avr-gcc-list] buggy variable naming with underscores
> Apologies, yes I see I have not read your post properly :S.
> Insufficientcoffee.
>
> I can't see anything that would cause your problem in the example.
>
> My only suggestion would be to write the code how it *should* work
> (that is,
> no -1), and if it acts wrong then check the generated assembly
> and/or look
> for an instance where nNextAdc is being scribbled.
>
> Also - if you can reduce your program to a broken example that
> just reads
> the ADC only (ie only main() and SIGNAL(SIG_ADC)), and you can
> paste the
> entire program here, then it would be easier for interested people
> to have a
> peek. There is too much probability of 'something else' breaking
> the code
> when only a single function is posted.
Ok, I simplified the code, it just reads all 8 ADC channels on the atmega8 and
sends them over the UART to the PC. Please try it out and see if you can
figure out why the channels are all shifted by 1 index in the array! Here is
the code.
cheers,
Jamie
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/signal.h>
#include <avr/pgmspace.h>
#include <stdio.h>
typedef unsigned char u08;
typedef char s08;
typedef unsigned short u16;
typedef short s16;
#define UART_CPU 16000000
#define UART_BAUD_RATE 57600
#define UART_BAUD_SELECT (UART_CPU/(UART_BAUD_RATE*16l)-1l)
volatile u08 UART_Ready;
volatile u08 UART_ReceivedChar;
u08 UART_RxChar;
u08* pUART_Buffer;
volatile u08 r_char;
volatile u08 data;
volatile u08 CharAvail;
volatile u08 nNextAdc;
volatile s16 ADChannels[8];
SIGNAL(SIG_UART_TRANS)
{
if (pUART_Buffer!=0)
{
pUART_Buffer++;
if (PRG_RDB(pUART_Buffer)==0)
{
pUART_Buffer = 0;
UART_Ready = 1;
return;
}
UDR = PRG_RDB(pUART_Buffer);
return;
}
UART_Ready = 1;
}
void UART_SendByte(u08 Data)
{
while(!UART_Ready);
UART_Ready = 0;
UDR = Data;
}
u08 UART_ReceiveByte(void)
{
while(!UART_ReceivedChar);
UART_ReceivedChar = 0;
return UART_RxChar;
}
void UART_Init(void)
{
UART_Ready = 1;
UART_ReceivedChar = 0;
pUART_Buffer = 0;
UCSRB = (BV(RXCIE) | BV(TXCIE) | BV(RXEN) | BV(TXEN));
UBRRL = (u08)UART_BAUD_SELECT;
sei();
}
SIGNAL(SIG_UART_RECV)
{
r_char = UDR;
data = r_char;
CharAvail = 1;
}
SIGNAL(SIG_ADC)
{
u08 lt;
u08 ht;
lt = ADCL;
ht = ADCH & 0x03;
ADChannels[nNextAdc]=(((u16)ht)<<8)|lt; //all channels shifted
by one array index when printed with printf below
//ADChannels[nNextAdc-1]=(((u16)ht)<<8)|lt; //this line fills array
correctly, but may corrupt memory
nNextAdc++;
if (nNextAdc==8) nNextAdc=0;
ADMUX = nNextAdc;
}
void INIT_Adc(void)
{
nNextAdc=0;
DDRC = 0;
PORTC = 0;
ADMUX = nNextAdc;
ADCSRA = 0xEF;
}
int main(void)
{
UART_Init();
INIT_Adc();
fdevopen(UART_SendByte, NULL, 0);
printf("GO");
while (1)
{
if (CharAvail == 1)
{
CharAvail = 0;
switch (data)
{
case '1':
{
printf("ADChannels[0]: %i\r\n",
(int)(ADChannels[0]));
printf("ADChannels[1]: %i\r\n",
(int)(ADChannels[1]));
printf("ADChannels[2]: %i\r\n",
(int)(ADChannels[2]));
printf("ADChannels[3]: %i\r\n",
(int)(ADChannels[3]));
printf("ADChannels[4]: %i\r\n",
(int)(ADChannels[4]));
printf("ADChannels[5]: %i\r\n",
(int)(ADChannels[5]));
printf("ADChannels[6]: %i\r\n",
(int)(ADChannels[6]));
printf("ADChannels[7]: %i\r\n\n",
(int)(ADChannels[7]));
}
break;
default:
break;
}
}
}
}