bimalkamal
Member level 1
Could anybody please give me a shift register(with latch) library / function written in C?
Thank you.
Thank you.
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Could anybody please give me a shift register(with latch) library / function written in C?
Thank you.
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 #define shift 8 sbit latch at RB1_bit; sbit clock at RB0_bit; sbit data_bit at RB3_bit; sbit latch_Direction at TRISB1_bit; sbit clock_Direction at TRISB0_bit; sbit data_bit_Direction at TRISB3_bit; void send_data(unsigned char temp); void main() { unsigned char a; latch_Direction=0; clock_Direction=0; data_bit_Direction=0; while(1){ for(a=0;a<255;a++) { send_data(a); Delay_ms(500); } } } void send_data(unsigned char temp) { unsigned char i; unsigned char dd; latch=0; clock=0; for(i=0;i<shift;i++){ dd=temp>>i; if(dd&1) data_bit=1; else data_bit=0; clock=1; clock=0; } latch=1; }
#ifdef F_CPU
#undef F_CPU
#endif
#define F_CPU 8000000L
#include<avr\io.h>
#include<util\delay.h>
#define SR_DDR DDRC
#define SR_PORT PORTC
#define SR_DS PC0
#define SR_SH PC1
#define SR_ST PC2
void SR_Init()
{
SR_DDR|=(1<<SR_DS)|(1<<SR_SH)|(1<<SR_ST);
SR_PORT&=~((1<<SR_DS)|(1<<SR_SH)|(1<<SR_ST));
}
void SR_DataHigh()
{
SR_PORT|=(1<<SR_DS);
}
void SR_DataLow()
{
SR_PORT&=~(1<<SR_DS);
}
void SR_Shift()
{
SR_PORT|=(1<<SR_SH);
SR_PORT&=~(1<<SR_SH);
}
void SR_Output()
{
SR_PORT|=(1<<SR_ST);
SR_PORT&=~(1<<SR_ST);
}
void SR_Send(uint8_t data)
{
for(uint8_t i=0;i<8;++i)
{
if(data & 0x80)
SR_DataHigh();
else
SR_DataLow();
SR_Shift();
data<<=1;
}
SR_Output();
}
int main()
{
uint8_t a=1;
SR_Init();
while(1)
{
SR_Send(a);
_delay_ms(500);
if(!a) a=1;
a<<=1;
}
return 0;
}
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 #define shift 8 sbit latch at RB1_bit; sbit clock at RB0_bit; sbit data_bit at RB3_bit; sbit latch_Direction at TRISB1_bit; sbit clock_Direction at TRISB0_bit; sbit data_bit_Direction at TRISB3_bit; void send_data(unsigned char temp); void main() { unsigned char a; latch_Direction=0; clock_Direction=0; data_bit_Direction=0; while(1){ for(a=0;a<255;a++) { send_data(a); Delay_ms(500); } } } void send_data(unsigned char temp) { unsigned char i; unsigned char dd; latch=0; clock=0; for(i=0;i<shift;i++){ dd=temp>>i; if(dd&1) data_bit=1; else data_bit=0; clock=1; clock=0; } latch=1; }
See https://www.edaboard.com/blog/1628/
Will it work in HItech C?