|
|
View previous topic :: View next topic |
Author |
Message |
Analyzer
Joined: 07 Sep 2003 Posts: 32
|
74HC595 and ULN2003 problem |
Posted: Mon Feb 05, 2007 1:46 am |
|
|
Dear All,
I'm trying to utilize 74HC595+ULN2003A+F628-04 with CCS. Circuit is simple, it is a basic counter.But i'm having trouble with the program. I modified standart 74595.c for 3 74HC595. Code is here :
Code: |
#include <16F628A.h>
#include <STDLIB>
#FUSES NOWDT //No Watch Dog Timer
#FUSES XT //Crystal osc <= 4mhz
#FUSES NOPUT //No Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES BROWNOUT //Reset when brownout detected
#FUSES MCLR //Master Clear pin enabled
#FUSES LVP //Low Voltage Programming on B3(PIC16) or B5(PIC18)
#FUSES NOCPD //No EE protection
#use delay(clock=4000000)
#IFNDEF EXP_OUT_ENABLE
#define EXP_OUT_ENABLE1 PIN_B0
#define EXP_OUT_ENABLE2 PIN_B3
#define EXP_OUT_ENABLE3 PIN_B5
#define EXP_OUT_CLOCK PIN_B1
#define EXP_OUT_DO PIN_B2
#define NUMBER_OF_74595 3
#ENDIF
void write_expanded_outputs(byte* eo, byte chipnum) {
byte i;
output_low(EXP_OUT_CLOCK);
switch(chipnum)
{
case 0 :
output_low(EXP_OUT_ENABLE1);
break;
case 1 :
output_low(EXP_OUT_ENABLE2);
break;
case 2 :
output_low(EXP_OUT_ENABLE3);
break;
}
for(i=1;i<=NUMBER_OF_74595*8;++i) {
if((*(eo+(NUMBER_OF_74595-1))&0x80)==0)
output_low(EXP_OUT_DO);
else
output_high(EXP_OUT_DO);
shift_left(eo,NUMBER_OF_74595,0);
output_high(EXP_OUT_CLOCK);
output_low(EXP_OUT_CLOCK);
}
switch(chipnum)
{
case 0 :
output_high(EXP_OUT_ENABLE1);
break;
case 1 :
output_high(EXP_OUT_ENABLE2);
break;
case 2 :
output_high(EXP_OUT_ENABLE3);
break;
}
}
void main()
{
char data[] = {63,6,91,79,102,109,124,7,127,111};
char data2[] = {0,63,6,91,79,102,109,124,7,127,111};
char data3[] = {0,0,63,6,91,79,102,109,124,7,127,111};
int8 thous,huns,tens,units;
int sayi = 549;
delay_ms(500);
output_high(PIN_A2);delay_ms(500);output_low(PIN_A2);delay_ms(500);
while(1)
{
units = 1;
tens = 0;
huns = 0;
write_expanded_outputs (&data[huns],0);
write_expanded_outputs (&data2[tens],1);
write_expanded_outputs (&data3[units],2);
units++;
delay_ms(1500);
}
}
|
As you see, code is simple. When i run this code; 001 can be seen on 7 seg displays, then goes off. So, where did i make mistake?
This is my Proteus file :
http://rapidshare.com/files/14981486/Counter_ULN.DSN.html
I printed some boards and populated components, so it is very hard to change my hardware.
Thanks for spendig time.
Analyzer |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Feb 05, 2007 2:36 am |
|
|
Are you really using a Low Voltage programmer ?
If not, change that fuse to NOLVP.
In CCS, an 'int' is an 8-bit unsigned integer. It can only hold a value
up to 255. To hold 549, you must declare it as a 'long' or 'int16',
which will make it a 16-bit unsigned integer. |
|
|
Analyzer
Joined: 07 Sep 2003 Posts: 32
|
|
Posted: Mon Feb 05, 2007 3:00 am |
|
|
Thank you PCM_Programmer,
I changed LVP->NOLVP and commented int sayi = 549; line with "//". By the way, what can you say about main problem? My "001" flashes on screen and does not display "002" in second cycle. Why?
Thanks in advance.
Analyzer |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Feb 05, 2007 3:05 am |
|
|
I have no way to view a Proteus file, so I can't really do anything. |
|
|
Analyzer
Joined: 07 Sep 2003 Posts: 32
|
|
Posted: Mon Feb 05, 2007 4:37 am |
|
|
Whoops! Sorry, I just posted my schematic :
Analyzer |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Mon Feb 05, 2007 5:17 am |
|
|
Code: | shift_left(eo,NUMBER_OF_74595,0); | You are shifting the original character array. After displaying the first time the character array will contain all zeroes, hence the blank display.
Do not shift the array but test the contents instead. |
|
|
Analyzer
Joined: 07 Sep 2003 Posts: 32
|
|
Posted: Mon Feb 05, 2007 6:21 am |
|
|
Dear ckielstra,
Thank you for spending time. I confess, i'm not good at C. I commented this line :
Code: |
//shift_left(eo,NUMBER_OF_74595,0);
|
But now i can not see 001 or any other character. So what do you advice?
Thanks in advance.
Analyzer |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Mon Feb 05, 2007 7:02 am |
|
|
Analyzer wrote: | I confess, i'm not good at C. I commented this line :
Code: |
//shift_left(eo,NUMBER_OF_74595,0);
|
But now i can not see 001 or any other character. So what do you advice? | I didn't say commenting the line was the solution, I was suggesting to change this line to something else.
Just curious, is this a school assignment and you copied the code from somewhere else? |
|
|
Analyzer
Joined: 07 Sep 2003 Posts: 32
|
|
Posted: Mon Feb 05, 2007 7:11 am |
|
|
Dear ckielstra,
Thank you for your kind reply. This is not a school homework, i'm an electronic amateur and trying to make a simple counter for my step motor test unit. I copied this code from here :
http://www.ccsinfo.com/forum/viewtopic.php?t=7851&highlight=74hc595
and tried to change a bit to use 3 x 74HC595 in cascade mode. Unfortunately, i couldn't find suitable example for my project.
So, i still could not find any suitable solution for that line.
Analyzer |
|
|
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
|
Posted: Mon Feb 05, 2007 9:15 am |
|
|
Try this. I think driver wants you to use a common enable pin.
Tie all 74HC595 pin 12 together, and then to a PIC pin.((enable))
Serial data you have OK on PIC pin RB2. into 74HC595 pin14 and then 9 to 14 and 9 to 14.
Last is clk which is also OK on RB1.
http://chaokhun.kmitl.ac.th/~kswichit/Led/schematic.pdf
I would use the drivers included with CCS and see if you can get the 74hc595
to produce the outputs you want. ie: use a known working driver.
C:\Program Files\PICC\Examples\EX_EXPIO.C
C:\Program Files\PICC\Drivers\74595.C
Here is some test code that works.
remove/change things that are different.
Code: | #INCLUDE <18F452>
#CASE
#USE DELAY(CLOCK=12000000)
//#USE DELAY(CLOCK=16000000) //********************************* ! ! ! ! ! !
#FUSES HS,NOWDT,NOPROTECT,NOLVP
#DEFINE VER_MAJOR 1
#DEFINE VER_MINOR 03
#USE RS232(BAUD=19200,XMIT=PIN_C5,INVERT,STREAM=DEBUG)
#USE RS232(BAUD=19200,XMIT=PIN_C5,INVERT,STREAM=STDERR)
#ZERO_RAM
#DEFINE EXP_IN_ENABLE PIN_E0
#DEFINE EXP_IN_CLOCK PIN_E1//shared clocks OK
#DEFINE EXP_IN_DI PIN_E2
#DEFINE NUMBER_OF_74165 2
#DEFINE EXP_OUT_ENABLE PIN_C0
#DEFINE EXP_OUT_CLOCK PIN_E1 //shared clocks OK
#DEFINE EXP_OUT_DO PIN_C2
#DEFINE NUMBER_OF_74595 2
#INCLUDE <74595>
#INCLUDE <74165>
//======================= MAIN ============================//
void main(void)
{
int16 exDataIn,exDataInOld,exDataOut;
setup_adc_ports(NO_ANALOGS);
set_tris_a(0);set_tris_b(0);set_tris_c(0);set_tris_d(0);set_tris_e(0);
fprintf(DEBUG,"STARTING ME Lab Test.\n\r");
fprintf(DEBUG,"FIRMWARE VERSION %u.%02u\n\r",VER_MAJOR,VER_MINOR);
exDataIn=0;exDataInOld=0;
do
{
read_expanded_inputs(&exDataIn);
if (exDataIn!=exDataInOld)
{
exDataInOld=exDataIn;
fprintf(DEBUG,"%lX ",exDataIn);
//write_expanded_outputs
exDataOut=exDataIn;//write_expanded_outputs is destructive. data is changed to zero
write_expanded_outputs(&exDataOut);//16MhZ & 8bits takes 129.5e-6 Sec.
}
} while (TRUE);
}
|
|
|
|
Analyzer
Joined: 07 Sep 2003 Posts: 32
|
|
Posted: Mon Feb 05, 2007 2:21 pm |
|
|
Dear treimey,
I changed my circuit like this :
But i still can not get result with 3 74HC595
Analyzer |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Feb 05, 2007 2:40 pm |
|
|
Your schematic doesn't show power and ground connections for any
of the chips. Do you have those hooked up ? For example, pin 8
of the ULN2003A chips all need to go to Ground. |
|
|
Analyzer
Joined: 07 Sep 2003 Posts: 32
|
|
Posted: Mon Feb 05, 2007 2:43 pm |
|
|
Dear PCM programmer,
This schematic is from proteus and Proteus automatically make VCC and GND connections. There is a programming fault in my project :(
Analyzer |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Feb 05, 2007 3:13 pm |
|
|
What code are you currently using to drive your latest schematic ? |
|
|
SimpleAsPossible
Joined: 19 Jun 2004 Posts: 21
|
Wrong code? |
Posted: Sun Feb 11, 2007 8:59 am |
|
|
I have done something similar. I used three '595s to drive the enable lines on other ICs, and I chained the '595s together the way you show in your schematic. Think of the chain of three '595s as a single 24-bit shift register.
I had a PIC with SPI, so it was easy -- SPI clock to the first '595 pin 11, SPI data to the first '595 pin 14, chain the '595s the way you did, and do three SPI_WRITE commands. You've got it a bit tougher. You'll need to manually toggle the clock and data lines.
As a side note, it looks like the code you have is for non-chained registers.
Since you say you're not good at C, I wrote this the brute-force way. I also am using your data[] array, which I didn't check for consistency.
Code: |
// --------- untested code ---------
#define LOAD pin_b0 // 16F628 pin 6
#define CLOCK pin_b1 // 16F628 pin 7
#define DATA pin_b2 // 16F628 pin 8
write_byte( int8 x )
{
int8 bitcount = 8;
do
{
if ( x % 1 ) // test least significant bit
output_high( DATA );
else
output_low( DATA );
output_high( CLOCK ); // clock data into shift register
output_low( CLOCK );
x >> 1; // point to next bit
bitcount--;
} while ( bitcount );
}
write_display( int16 x )
{
static char data[] = {63,6,91,79,102,109,124,7,127,111}; // static to avoid initializing every time
write_byte( data[ (x) % 10 ] ); // write units
write_byte( data[ (x/10) % 10 ] ); // write tens
write_byte( data[ (x/100) % 10 ] ); // write hundreds
output_high( LOAD ); // load shift registers into output latches
output_low( LOAD );
}
main()
{
int16 counter = 0;
while ( TRUE )
{
write_display( counter );
counter++;
}
}
|
You don't show how the LEDs are wired, so I assumed LSB-first would work. To swap it, change (x%1) to (x%128) and change x>>1 to x<<1. Change the comment to "test most significant bit" so future edits are less confusing. Or you can reverse the bits in your data[] array. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|