CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Display counting number on dot 5x7 matrix and 16F628
Goto page 1, 2, 3  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
wala.iswara



Joined: 10 Jun 2013
Posts: 21

View user's profile Send private message

Display counting number on dot 5x7 matrix and 16F628
PostPosted: Tue Jun 11, 2013 12:24 am     Reply with quote

Hallo,
I want to display counting number 0 to 9 on 5x7 dot matrix. The mcu is 16F628A.
I use the following code, successfully compiling, but not showing number with Proteus.
Can anyone tell me why? Or have any code for reference?
Code:

//counting number 0 to 9 on 5x7 dot matrix with PIC16F628
//port B for row, Cathode matrix
//port A for column, Anode matrix

#include <16F628.h>

#fuses HS,NOWDT,NOPUT,NOPROTECT,NOBROWNOUT,NOLVP
#use delay(clock=4000000)

#INT_TIMER0

unsigned short i, count;
unsigned short mask(unsigned short num) {
switch (num) {
case 0 : return ( 0x3E, 0x41, 0x41, 0x41, 0x3E );
case 1 : return ( 0x00, 0x01, 0x7F, 0x21, 0x00 );
case 2 : return ( 0x31, 0x49, 0x45, 0x45, 0x27 );
case 3 : return ( 0x36, 0x49, 0x49, 0x49, 0x49 );
case 4 : return ( 0x04, 0x4F, 0x24, 0x14, 0x1C );
case 5 : return ( 0x4E, 0x51, 0x51, 0x51, 0x71 );
case 6 : return ( 0x4E, 0x51, 0x51, 0x51, 0x3E );
case 7 : return ( 0x70, 0x50, 0x48, 0x47, 0x40 );
case 8 : return ( 0x36, 0x49, 0x49, 0x49, 0x36 );
case 9 : return ( 0x3E, 0x45, 0x45, 0x45, 0x39 );
 }
}

void main(){
 
  setup_comparator(0x0ff07); // CCP off
  set_tris_a(0x00);
  set_tris_b(0x00);
 
  while(TRUE){
  for (i=0;i<5;i++)          // Displaying i on 5x7 dot matrix
  count=mask(i);
  count++;
  if (i>9)i=0;
  delay_ms(500);
 
  }
 }
Ttelmah



Joined: 11 Mar 2010
Posts: 19518

View user's profile Send private message

PostPosted: Tue Jun 11, 2013 3:24 am     Reply with quote

There are several things wrong:

1) Forget Proteus..... It commonly has a lot of problems with the PIC chips, and unless you already know how to get PIC's working, can mislead you as to what works/doesn't.... This is a 'sticky' at the top of the forum.

2) HS, means a _greater_ than 4MHz crystal is going to be used. Then you have your clock set for 4MHz. Not compatible. Have you actually got the chip working, and done a simple 'flash an LED' test first.

3) #INT_TIMER0, means that the following routine is the handler for a timer0 interrupt. It should be immediately in front of the routine to be used, and then you are not using this at all. What the effect would be on the compiler is 'dubious'.....

4) You have 'i' being tested for being >9, yet it is counting 0 to 4.... One or the other is wrong.

5) Then you say 'displaying i on 5x7 dot matrix', but nowhere are you actually outputting anything to the matrix.

6) You have a line remarked with 'CCP off', yet it is the comparator being controlled here, not the CCP. Also using 'values' like this is poor practive. Use the defined keywords, so the command becomes self documenting.

At the moment the code really does nothing, do even with the limitations of Proteus, it is probably giving the right answer, that nothing will happen...

Best Wishes
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Tue Jun 11, 2013 4:10 am     Reply with quote

And a few other huge ones:
Code:
  for (i=0;i<5;i++)          // Displaying i on 5x7 dot matrix
  count=mask(i);
  count++;
  if (i>9)i=0;
  delay_ms(500);
7) Which statements do you expect to be executed in the for-loop?
The C language defines that only the next statement will be executed, here your line 'count = mask(i);'.
If you want more instructions to be executed then put them inside a '{}' block.
Note that it is good practice to ALWAYS put '{}' characters around the part that you want to be executed in the for-loop, even when it is just one line. This will help you to see clearly which part is being looped.

Code:
unsigned short i, count;
8) You do like short variables, don't you?
When you look up the 'short' type in the manual then you will see that for the CCS compiler this has a size of only 1 bit.... It can only contain 0 or 1, not all the larger values you want to store in them.
The size of types like 'int', 'short' and 'double' depends on the compiler. You will save yourself a lot of trouble by not using these types but use a type like int1, int8, int16 which will be the same on every compiler you ever use.

Code:
unsigned short mask(unsigned short num) {
switch (num) {
case 0 : return ( 0x3E, 0x41, 0x41, 0x41, 0x3E );
9) You have specified that the function will return 1 'unsigned short' but then in the return statement you are returning 5 values?!?!?!?

Such a small program. So many errors.
I'm afraid you have a long way to go.

I suggest you have a look at the MPLAB simulator tool. You can use this to single step through your program and examine exactly what is happening. It will speed up your debugging process dramatically.
wala.iswara



Joined: 10 Jun 2013
Posts: 21

View user's profile Send private message

PostPosted: Tue Jun 11, 2013 9:13 am     Reply with quote

Dear Ttelmah & ckielstra,

Thanks a lot.
Don't worry about the mcu, i was make 1 digit counter on 7 segment with my 16F628A, by 4Mhz clocking.
And now I am trying with dot matrix LED.

I am trying my new code. Still not working.

here is my new code:
Code:

//counting number 0 to 9 on 5x7 dot matrix with PIC16F628
//port B for row, Cathode matrix
//port A for column, Anode matrix


#include <16F628.h>

#fuses HS,NOWDT,NOPUT,NOPROTECT,NOBROWNOUT,NOLVP
#use delay(clock=4000000)


#byte port_b = 6


int8 i, count;
int8 mask(unsigned short num) {
switch (num) {
case 0 : return  0x3E, 0x41, 0x41, 0x41, 0x3E ;
case 1 : return  0x00, 0x01, 0x7F, 0x21, 0x00 ;
case 2 : return  0x31, 0x49, 0x45, 0x45, 0x27 ;
case 3 : return  0x36, 0x49, 0x49, 0x49, 0x49 ;
case 4 : return  0x04, 0x4F, 0x24, 0x14, 0x1C ;
case 5 : return  0x4E, 0x51, 0x51, 0x51, 0x71 ;
case 6 : return  0x4E, 0x51, 0x51, 0x51, 0x3E ;
case 7 : return  0x70, 0x50, 0x48, 0x47, 0x40 ;
case 8 : return  0x36, 0x49, 0x49, 0x49, 0x36 ;
case 9 : return  0x3E, 0x45, 0x45, 0x45, 0x39 ;
 }
}

void display_number(){
for (i=0; i<5;i++)
port_b = i;
port_b=mask(i);

output_high(PIN_A0);
delay_ms(100);
output_low(PIN_A0);

output_high(PIN_A1);
delay_ms(100);
output_low(PIN_A1);

output_high(PIN_A2);
delay_ms(100);
output_low(PIN_A2);

output_high(PIN_A3);
delay_ms(100);
output_low(PIN_A3);

output_high(PIN_A4);
delay_ms(100);
output_low(PIN_A4);
}


void main(){

  setup_comparator(0x0ff07); // Comparator off
  set_tris_a(0x00);
  set_tris_b(0x00);
  port_b=0;

  while(TRUE){
  display_number();
  if (i>9)i=0;
  delay_ms(1000);

  }
 }
Ttelmah



Joined: 11 Mar 2010
Posts: 19518

View user's profile Send private message

PostPosted: Tue Jun 11, 2013 9:20 am     Reply with quote

mask, is still only accepting a short....

Then a function returning an int8, returns _one_ number. Yours attempts to return five.

Then you output 'i' to port_b, and in the next instruction output mask(i). You will never see the first value.

Still have 4MHz clock and HS selected.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Tue Jun 11, 2013 3:41 pm     Reply with quote

When programming it is very important to have an eye for details. You make some big errors but also a lot of tiny errors.
wala.iswara wrote:
Dont worry obout the mcu, i was make 1 digit counter on 7 segment with my 16F628A, by 4Mhz clocking.
Code:

#include <16F628.h>
Your include is not for the same processor as your hardware, only a letter 'A' different, but still a potential problem.

Code:
setup_comparator(0x0ff07); // Comparator off
You still didn't follow Ttelmah's suggestion. The problem in this line is that it is using a 'magic' value. Difficult to understand but can also change by CCS in the next compiler version and then you will have a very difficult problem to find. Change to:
Code:
setup_comparator(NC_NC_NC_NC); // Comparator off


Code:
output_high(PIN_A4);
We don't know your circuit, but this is a potential problem. Pin A4 is an open collector output only, meaning that you can not actively drive it high.

Code:
for (i=0; i<5;i++)
port_b = i;
port_b=mask(i);
You didn't fix issue number 7 from my previous post. I wonder why I do waste my time here. I'm off to bed now.
wala.iswara



Joined: 10 Jun 2013
Posts: 21

View user's profile Send private message

PostPosted: Tue Jun 11, 2013 7:12 pm     Reply with quote

Dear Ttelmah & ckielstra,
I learning with PCW C Compiler IDE Version 3.28, have not #Include < xxx.h> for 16F628A. So i use #include<xxx.h> for 16F628.
Code:
setup_comparator(NC_NC_NC_NC_); // Comparator off
Yes, thanks for refreshing.

Quote:
ckielstra:
You didn't fix issue number 7 from my previous post. I wonder why I do waste my time here. I'm off to bed now.
I am trying, but OK lets relax for a moment. Its really confusing me. Smile
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Wed Jun 12, 2013 1:38 am     Reply with quote

THE most important question.

Have you thrown Proteus/ISIS away?
(Very first advice given)

When you are using real chips, say so clearly, then many of us will try to help.

Mike
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Wed Jun 12, 2013 1:56 am     Reply with quote

wala.iswara wrote:
I learning with PCW C Compiler IDE Version 3.28, have not #Include < xxx.h> for 16F628A. So i use #include<xxx.h> for 16F628.
v3.28 is the version number for the IDE, not for the compiler. The compiler has a version number like 3.xxx and can be found at the top of the list file (*.lst) in your project directory.

The PIC16F628A is from around 2003, this means you are using a compiler of more than 10 years old?
It should work for the PIC16F628, the differences are small, but you are taking some small risks here. Have a look at appendix C of the PIC16F628A data sheet for a list with differences between the two devices.
wala.iswara



Joined: 10 Jun 2013
Posts: 21

View user's profile Send private message

PostPosted: Wed Jun 12, 2013 8:15 pm     Reply with quote

Hallo,

@Mike:
I was make a minimum system for my 16F628A.
http://www.flickr.com/photos/55060929@N06/

@ckielstra:
Yes. The version number is the version number of the IDE.
I was fixed issues no. 7. And result is LED blinking a cycle.

@Ttelmah:
Till now, i just have 4Mhz x-tall. I will change after i can get a higher ones.
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Thu Jun 13, 2013 9:46 am     Reply with quote

OK. So you've got real hardware.

Driving an LED matrix has been discussed loads of times on this forum.
Do a search to see how others have done it.

You should be able to adapt what's in the link below to your circuit.

http://www.ccsinfo.com/forum/viewtopic.php?t=47436&start=0&postdays=0&postorder=asc&highlight=

Give it a try.
When/if you get stuck, show us what you've done and we'll try to help.

Don't worry about the crystal. 4MHz should be more than fast enough at this stage. Just change the fuse.

Mike
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Thu Jun 13, 2013 9:49 am     Reply with quote

I actually have working code for this...
it was a university project in 2005..... ill see if i can dig it up.

G.
_________________
CCS PCM 5.078 & CCS PCH 5.093
Ttelmah



Joined: 11 Mar 2010
Posts: 19518

View user's profile Send private message

PostPosted: Thu Jun 13, 2013 10:10 am     Reply with quote

wala.iswara wrote:
Hallo,

@Mike:
I was make a minimum system for my 16F628A.
http://www.flickr.com/photos/55060929@N06/

@ckielstra:
Yes. The version number is the version number of the IDE.
I was fixed issues no. 7. And result is LED blinking a cycle.

@Ttelmah:
Till now, i just have 4Mhz x-tall. I will change after i can get a higher ones.


Aaargh......


There is nothing whatsoever wrong with using a 4MHz crystal, but you _must_ set the oscillator to the correct setting to use this.

HS, is the 'high speed' oscillator, and is not rated to operate a 4MHz crystal. Using it tends to result in the crystal being over-driven, and increase the tendency to lock onto an overtone, rather than the correct frequency.
XT, is the correct oscillator fuse for 4MHz. _USE IT_.

Best Wishes
wala.iswara



Joined: 10 Jun 2013
Posts: 21

View user's profile Send private message

PostPosted: Fri Jun 14, 2013 12:37 am     Reply with quote

Hallo,

@Ttelmah & Mike:
Ha ya ya...
The #fuses. Yes, my mistake! XT is for 4 Mhz, not HS.
Thanks alot.

Any way,
i just simplify my code, and use a 4017 shift register. It just for show 1 number, and not counting. It is work.

Code:
#include <16F628.h>

#fuses XT,NOWDT,NOPUT,NOPROTECT,NOBROWNOUT,NOLVP
#use delay(clock=4000000)

#define Clock_4017 PIN_A0 // -> to pin_14 4017
#define Reset_4017 PIN_A1 // -> to pin_15 4017

#byte port_b=6

int8 mask[]= { 0xFE, 0xFE, 0x00, 0xDE, 0xDE }; // 1

void main()
{
int8 i;

// ADD ALL PIC TRIS SETUP
  setup_comparator(NC_NC_NC_NC); // Comparator OFF
  set_tris_a(0);
  set_tris_b(0);

delay_ms(500);

while (TRUE){

for (i = 0; i <5; i++) {

 port_b = i;
 port_b=mask[i];
 delay_ms(25);
    {
    // Clock 4017
    output_high(PIN_A0);
    delay_ms(5);
    output_low(PIN_A0);
    delay_ms(5);
    }
   }
    // Reset 4017
    output_high(PIN_A1);
    delay_ms(5);
    output_low(PIN_A1);
    delay_ms(5);
  } //End while
}
wala.iswara



Joined: 10 Jun 2013
Posts: 21

View user's profile Send private message

PostPosted: Fri Jun 14, 2013 2:58 am     Reply with quote

Quote:
i just simplify my code, and use a 4017 shift register.
Sorry, 4017 decade counter. Not shift register.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2, 3  Next
Page 1 of 3

 
Jump to:  
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