Hi folks, newbie Karl here.
I'm not having much luck reading data stored on program memory in my ATtiny5. It works great on an ATtiny85.
I define an array of data in Program Memory:
const uint8_t sinewave[] PROGMEM= //64 values
{
0x80,0x83,0x86,0x89,0x8c,0x8f,0x92,0x95,0x98,0x9c,0x9f,0xa2,0xa5,0xa8,0xab,0xae,
0xb0,0xb3,0xb6,0xb9,0xbc,0xbf,0xc1,0xc4,0xc7,0xc9,0xcc,0xce,0xd1,0xd3,0xd5,0xd8,
0xda,0xdc,0xde,0xe0,0xe2,0xe4,0xe6,0xe8,0xea,0xec,0xed,0xef,0xf0,0xf2,0xf3,0xf5,
0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfc,0xfd,0xfe,0xfe,0xff,0xff,0xff,0xff,0xff
};
I set up timer0 and enter a simple delay loop:
int main(void)
{
int i;
// PB1 output
DDRB = (1<<PB1);
// Timer0 in mode 14, fast PWM with ICR0 as top.
// Enable OC0A and OC0B, lower mode bits
TCCR0A = (1<<COM0A1) | (1<<COM0B1) | (1<<WGM01);
// Set top to 1000
ICR0 = 1000;
// Start timer with prescaler 1:8 and set upper mode bits
TCCR0B = (1<<CS01) | (1<<WGM03) | (1<<WGM02);
while(1)
{
OCR0A = pgm_read_byte(&sinewave[i]);
i++;
i %= 64;
_delay_ms(1);
}
}
Here is code from the disassembler windows:
Code generated for ATTiny85 :
OCR0A = pgm_read_byte(&sinewave[i]);
000000CA LDS R30,0x0066 Load direct from data space
000000CC LDI R31,0x00 Load immediate
000000CD SUBI R30,0xE2 Subtract immediate
000000CE SBCI R31,0xFF Subtract immediate with carry
000000CF LPM R30,Z Load program memory
000000D0 OUT 0x29,R30 Out to I/O location
Code generated for ATTiny5 :
OCR0A = pgm_read_byte(&sinewave[i]);
00000073 MOV R30,R20 Copy register
00000074 MOV R31,R21 Copy register
00000075 SUBI R30,0xEA Subtract immediate
00000076 SBCI R31,0x7F Subtract immediate with carry
00000077 LDD R30,Z+0 Load indirect with displacement
00000078 LDI R31,0x00 Load immediate
00000079 OUT 0x27,R31 Out to I/O location
0000007A OUT 0x26,R30 Out to I/O location
My Observations on the ATTiny5 code:
sinewave[] starts at address 0x0016.
"i" seems to be held in pair R20,21.
"Z" register sems to be held in pair R30,31.
If "i"=0, then after step 75, the Z register value is 0x0016. After step 76, the Z register value is 0x8016. Then the LDD command retruns 0x00.
So, that is my problem, as I understand it. Any insight would be much appreciated. Thanks!