PIC18F4520 4 Digit 7 Segment using 74hc595 Shift Register

Status
Not open for further replies.

imranahmed

Advanced Member level 3
Joined
Dec 4, 2011
Messages
817
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,298
Location
Karachi,Pakistan
Activity points
6,493
Please let me know I copied a code from internet for subject project and did some changing according to mikroC compiler, it is compiled and run successfully but only one digit is count-up not all other three digits are displaying. Do you know any other code or any link or hint. Two shift registers are used I attached here and also code is attached. Code is written in mikroC.
If I want to use common cathode display so what will changing required in column I also tried digits values in array for common cathode but not working.

Code:
#define SCLK  PORTC.B0         //Serial Clock
#define SDOUT PORTC.B1        //Serial Data Out
#define SDIN  PORTC.B2         //Serial Data In
#define CS0   PORTC.B3         //Chip Select
#define CS1   PORTC.B4
//---------------------------DIRECTION------------------------------------------
#define SCLK_dir   TRISC.B0
#define SDOUT_dir  TRISC.B1
#define SDIN_dir   TRISC.B2
#define CS0_dir    TRISC.B3
#define CS1_dir    TRISC.B4
//------------------------------------------------------------------------------

unsigned int size,offset,Rx,j,pos,count,temp, adc_value; //store output value from Analog Read function
char _data=0;
char* strNum[10];            //Character buffer (see itoa prototype)
//-------------------------Common Anode-----------------------------------------
                                // 0 , 1  ,  2 ,  3 ,  4 ,  5 ,  6 ,  7 ,  8 ,  9
unsigned const char digit[10] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
unsigned const char column[8] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
unsigned const char noDigits = 4;

void SPI_init(void)
{
  SCLK_dir = 0;
  SDOUT_dir = 0;
  SDIN_dir = 1;
  CS0_dir  = 0;
  CS1_dir  = 0;
}
//--------------------------TX: transmit data-----------------------------------//
void SPI_write(char send)
{
  for(j=0;j<8;j++)
  {
    SCLK = 0;
    SDOUT = ((send << j) & 0x80) ? 1 : 0;   //MSB first.
    SCLK = 1;
  }
}
//--------------------------RX: recieve data------------------------------------//
char SPI_read(void)
{

  for(Rx = 0 ; Rx < 8 ; Rx++ )
  {
    SCLK = 0;
    _data += (SDIN << (7-Rx));  //MSB first.
    SCLK = 1;
  }

  return _data;
}

void SR_dataSend(char _data)
{
  CS0 = 0;
  SPI_write(_data);
  CS0 = 1;
}
//------------------------------------------------------------------------------
void SR_colSend(char col)
{
  CS1 = 0;
  SPI_write(col);
  CS1 = 1;
}
//------------------------------------------------------------------------------
void SR_colSelect(char col)
{
  SR_colSend(column[col]);    //Turn particular column On
  Delay_ms(5);
  SR_colSend(0);              //...then turn it Off
}
void segment_display(int number)
{

  IntToStr(number,strNum);    //Change from Int to C-String (stdlib function)

  size = strlen(strNum);               //<string.h> function
  offset = noDigits - size;

  for(pos=0; pos < size; pos++)
  {
    char index = strNum[pos] - 48;          //Decimal 48 is 0 in ASCII

    SR_dataSend(digit[index]);
    SR_colSelect(pos + offset);
  }
}



void main() {
ADCON0 = 0x00;
ADCON2 = 0x26;    //
OSCCON = 0x74;
// OSCTUNE = 0x40;   // osctune bit 6 is used to activate PLL for internal oscillator
TRISA  = 0x01;      /*set as input port*/
TRISC  = 0x00;

SPI_init();

while(1){

for(count=0; count<10000; count++)
    {
     segment_display(count);
     Delay_ms(20);
    }
count=0;
}
}
 

Attachments

  • SR_7Segment_Schematic.JPG
    119.4 KB · Views: 467
Last edited:

you wrote it within the QUOTE "KlausST said"
... which means KlausST has written the text....

*****

Now we know your code...
So it´s time for a detailed error description.

Klaus
Dear klaus,

Previous is solved now thank you for asking more about it.

Now another issue I am facing that I have device of temperature controller for controlling fan and displays some values on 4-digit 7-segments display, the device do not display temperature but display some strange (not garbage) values these values are useful for my project I mean I do not want temperature values, the values display by device is as 0, 1, 1.1, 1.2, 1.3 till 14.9 then 15 to 15.9 is not included after 14.9 then 16, 16.1, 16.2, 16.3........22.9 then 100. I applied a reverse engineering on device and attached a variable resistor on ADC input of PIC18f4520 and measure adc voltage on every display values 0 to 100.

For program another PIC18f4520 I made a code of integer array of values 0 to 100 they are 211 values in array now main issue is how I program on my PIC18f4520 same values. for example if I set variable resistor on device PIC18f it shows 3.5, exactly if I attached my controller then same value should be display.
Please do not worry about decimal point I will lit-up directly on 7-segment it is nothing to do with my code.

Code:
unsigned int const VALUES[] = {0,10,11,12,12,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,44,45,46,47,48,49,
                               50,51,52,53,54,55,56,57,58,59,
                               60,61,62,63,64,65,66,67,68,69,
                               70,71,72,73,74,75,76,77,78,79,
                               80,81,82,83,84,85,86,87,88,89,
                               90,91,92,93,94,95,96,97,98,99,
                               100,101,102,103,104,105,106,107,108,109,
                               110,111,112,113,114,115,116,117,118,119,
                               120,121,122,123,124,125,126,127,128,129,
                               130,131,132,133,134,135,136,137,138,139,
                               140,141,142,143,144,145,146,147,148,149,
                               160,161,162,163,164,165,166,167,168,169,
                               170,171,172,173,174,175,176,177,178,179,
                               180,181,182,183,184,185,186,187,188,189,
                               190,191,192,193,194,195,196,197,198,199,
                               200,201,202,203,204,205,206,207,208,209,
                               210,211,212,213,214,215,216,217,218,219,
                               220,221,222,223,224,225,226,227,228,229,
1000};
 

It is a bit hard to understand your new requirement but I think you want to turn some measurement you are making through the ADC and use that as an index into your 'VALUES' array.
What is the range of values that yuo wil get from your ADC? What is the lowest ADC value that you want to have display as '0'; what is the highest ADC value that you want to display as '100' (and no, that is not the same question as the first one)?
Susan
 

Actually I am doing reverse engineering of a Humidity Sensor it uses PIC18F4520 and LCD is attached to it and displaying values 0 to 100 but not all values are displayed on LCD values starts from 0 then 1.0 then 1.1 then 1.2 till 14.9, values from 15.0 to 15.9 are skipped not displaying, after 14.9 then 16.0 then 16.1 then 16.2 till 22.9 then 100.0. I noted that they are 212 values. I want to make a same device. Thermocouple is attached to ADC pin. External Crystal is used 20MHz.
 

You have not answered the last set of my questions about the relationship between the ADC values and what you want to see displayed as '0' through to '100' (with gaps).
Having gaps in the display of a humidity sensor really does not make sense to me but if that your design then....
What is the humidity sensor that you are using?
(I must admit that I am rather dubious about what you are doing - it does sounds a bit like trying to make a 'cheap knock-off product'.)
Susan
 
I am also want to about values I seen in the display and I want to make similar device but leave it, it is time wasting. I am starting new thread for learning SPWM by using dsPIC30F2010. If you have knowledge about it please guide me.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…