Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Shift Register Library for C

Status
Not open for further replies.

bimalkamal

Member level 1
Joined
Nov 27, 2011
Messages
32
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
1,529
Could anybody please give me a shift register(with latch) library / function written in C?

Thank you.
 

Probably - what are you trying to achieve?

what do you want to shift, a port, a byte, a non-fixed-size word? Serial input, parallel load? Input/load from where?

Do you need to just shift, rotate, do you need an overflow/underflow bit output?
 

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;
   }


See https://www.edaboard.com/blog/1628/
 
i don't know which shift register you are talking about ... if 8-bit is ok then here i have written a program for 8-bit 74595 shift register

Code:
#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;
}

Note: the processor i have used is ATMEGA8
 
Last edited by a moderator:


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?
 


Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top