Hello Fellow AVRfreaks,
My issue is as follows. Got some code to drive a shift reg with my attiny13, but when I compile the code, it seems like the compiler (microchip studio) is making changes to the bits.. Here is the code...
#include <avr/io.h>
//#define F_CPU 1000000
#include <util/delay.h>
/***************************************
Configure Connections
****************************************/
#define HC595_PORT PORTB
#define HC595_DDR DDRB
#define HC595_DS_POS PB0 //Data pin (DS) pin location
#define HC595_SH_CP_POS PB1 //Shift Clock (SH_CP) pin location
#define HC595_ST_CP_POS PB2 //Store Clock (ST_CP) pin location
#define HC595_OE_POS PB3 //Output Enable
/***************************************
Configure Connections ***ENDS***
****************************************/
//Initialize HC595 System
void HC595Init()
{
//Make the Data(DS), Shift clock (SH_CP), Store Clock (ST_CP) lines output
HC595_DDR|=((1<<HC595_OE_POS)|(1<<HC595_SH_CP_POS)|(1<<HC595_ST_CP_POS)|(1<<HC595_DS_POS));
}
//Low level macros to change data (DS)lines and (OE) lines
#define HC595DataHigh() (HC595_PORT|=(1<<HC595_DS_POS))
#define HC595DataLow() (HC595_PORT&=(~(1<<HC595_DS_POS)))
#define HC95_OE_High() (HC595_PORT|=(1<<HC595_OE_POS))
#define HC95_OE_Low() (HC595_PORT&=(~(1<<HC595_OE_POS)))
//Sends a clock pulse on SH_CP line
void HC595Pulse()
{
//Pulse the Shift Clock
HC595_PORT|=(1<<HC595_SH_CP_POS);//HIGH
_delay_loop_1(1);
HC595_PORT&=(~(1<<HC595_SH_CP_POS));//LOW
}
//Sends a clock pulse on ST_CP line
void HC595Latch()
{
//Pulse the Store Clock
HC595_PORT|=(1<<HC595_ST_CP_POS);//HIGH
_delay_loop_1(1);
HC595_PORT&=(~(1<<HC595_ST_CP_POS));//LOW
_delay_loop_1(1);
}
void HC595Write(uint8_t data)
{
//Send each 8 bits serially
//Order is MSB first
for(uint8_t i=0;i<8;i++)
{
//Output the data on DS line according to the
//Value of MSB
if(data & 0b10000000)
{
//MSB is 1 so output high
HC595DataHigh();
}
else
{
//MSB is 0 so output high
HC595DataLow();
}
HC595Pulse(); //Pulse the Clock line
data=data<<1; //Now bring next bit at MSB position
}
//Now all 8 bits have been transferred to shift register
//Move them to output latch at one
HC595Latch();
}
/*
Simple Delay function approx 0.5 seconds
*/
void Wait()
{
for(uint8_t j = 0; j < 5;j++)
for(uint8_t i=0;i<=200;i++)
{
_delay_loop_1(0);
}
}
int main()
{
uint8_t led_pattern[8]={
0b10000000,
0b01000000,
0b00100000,
0b00010000,
0b00001000,
0b00000100,
0b00000010,
0b00000001,
};
uint8_t led_pattern2[8]={
0b00000010,
0b00000100,
0b00001000,
0b00010000,
0b00100000,
0b01000000,
0b10000000,
0b00000000
};
uint8_t led_pattern3[8]={
0b10000000,
0b11000000,
0b11100000,
0b11110000,
0b11111000,
0b11111100,
0b11111110,
0b11111111,
};
uint8_t led_pattern4[8]={
0b11111110,
0b11111100,
0b11111000,
0b11110000,
0b11100000, //<<<<<<<<<<< HERE IS THE ISSUE <<<<<<<<<<<<<<
0b11000000,
0b10000000,
0b00000000
};
//Initialize HC595 system
HC595Init();
HC95_OE_Low();
uint8_t i=0;
while(1)
{
for(i=0;i<8;i++)
{
HC595Write(led_pattern[i]); //Write the data to HC595
Wait(); //Wait
}
for(i=0;i<8;i++)
{
HC595Write(led_pattern2[i]); //Write the data to HC595
Wait(); //Wait
}
for(i=0;i<8;i++)
{
HC595Write(led_pattern3[i]); //Write the data to HC595
Wait(); //Wait
}
for(i=0;i<8;i++)
{
HC595Write(led_pattern4[i]); //Write the data to HC595
Wait(); //Wait
}
}
return 0;
}
The issue is in led_pattern4. It clearly states what i want my bits to do, but when it gets to array location 4 it goes AWOL. Here is the watch window for that array:

if we compare the the bits in the array with the bits in the watch window, something does not add-up. WHAT AM I DOING WRONG? I have tested it with Proteus and physically with the attiny13 and they behave in this way. WHY?
uint8_t led_pattern4[8]={
0b11111110, // = fe OK
0b11111100, // = fc OK
0b11111000, // = f8 OK
0b11110000, // = f0 OK
0b11100000, // = 80 NOT OK <<<<<<<<<<< HERE IS THE ISSUE <<<<<<<<<<<<<<
0b11000000, // = 40 NOT OK
0b10000000, // = 20 NOT OK
0b00000000 // = 10 NOT OK
};
Any assistance in this issue will very much be appreciated.





