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 support@ccsinfo.com

blinking led - my code does not work

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Luis123migueL



Joined: 17 Jul 2019
Posts: 6

View user's profile Send private message

blinking led - my code does not work
PostPosted: Thu Jul 18, 2019 12:12 am     Reply with quote

Hello everyone!!

I started to learn programing with ccs compiler many days ago.

I want to blink 4 LEDs, but i have problems.

If you see the code below, it works perfectly. I simulated it using Proteus 8 and this code works like i want.
Code:

#include<16f628.h>
#fuses XT,NOWDT
#use delay(clock=4000000)
#use standard_io(B)

void juego1()
{
output_b(0b00000101);
delay_ms(1000);
output_b(0b00001010);
delay_ms(1000);
}

void juego2()
{
output_b(0b00001001);
delay_ms(1000);
output_b(0b00000110);
delay_ms(1000);
}

void main()
{
output_low(pin_B0);
output_low(pin_B1);
output_low(pin_B2);
output_low(pin_B3);

While(true)
  {
   juego1();
   juego2();
  }
}

The problem is when i use variable to do the same idea.
check the code below:
When i simulate this, the LEDs blink at different speed like i want, i think it works at speed clock but i don't know.
please help me!!
Code:

#include<16f628.h>
#fuses XT,NOWDT
#use delay(clock=1000000)
#use standard_io(B)

void juego1(int a)
{
output_b(0b00000101);
delay_ms(a);
output_b(0b00001010);
delay_ms(a);
}

void juego2(int b)
{
output_b(0b00001001);
delay_ms(b);
output_b(0b00000110);
delay_ms(b);
}

void main()
{
int time1=1000,time2=1000;

output_low(pin_B0);
output_low(pin_B1);
output_low(pin_B2);
output_low(pin_B3);

While(true)
  {
   juego1(time1);
   juego2(time2);
  }
}

pdt: I speak spanish and use google translate to explain my idea, sorry if the idea is not clear.
Peace!!!
MassaM



Joined: 08 Jun 2019
Posts: 31

View user's profile Send private message Visit poster's website

PostPosted: Thu Jul 18, 2019 6:32 am     Reply with quote

Hi,

Humble Opinion:

Proteus! Don't count on it. Besides, it is a simple project and you should build it on a breadboard at least. You will learn practically much more than just simulating.

Solution:

Use your own function. Code examples for both versions below.
Both were tested and were working with the same blink rate.

With own function - Working with the same blink rate! Smile

Code:
#include<16f628.h>
#fuses XT,NOWDT
#use delay(clock=1000000)
#use standard_io(B)

// With own function - Working with the same blink rate! :)

void __delay_ms(unsigned int16 time)
{
   while(time > 0)
   {
    delay_ms(1);
    time--;
   }
}

void juego1(unsigned int16 a)
{
   output_b(0b00000101);
   __delay_ms(a);
   output_b(0b00001010);
   __delay_ms(a);
}
void juego2(unsigned int16 b)
{
   output_b(0b00001001);
   __delay_ms(b);
   output_b(0b00000110);
   __delay_ms(b);
}

void main()
{
   unsigned int16 time1=1000,time2=1000;

   output_low(pin_B0);
   output_low(pin_B1);
   output_low(pin_B2);
   output_low(pin_B3);

   While(true)
   {
      juego1(time1);
      juego2(time2);
   }
}


Without own function

Code:


#include<16f628.h>
#fuses XT,NOWDT
#use delay(clock=1000000)
#use standard_io(B)

// Without own function

void juego1()
{
   output_b(0b00000101);
   delay_ms(1000);
   output_b(0b00001010);
   delay_ms(1000);
}

void juego2()
{
   output_b(0b00001001);
   delay_ms(1000);
   output_b(0b00000110);
   delay_ms(1000);
}

void main()
{
   output_low(pin_B0);
   output_low(pin_B1);
   output_low(pin_B2);
   output_low(pin_B3);

   While(true)
   {
      juego1();
      juego2();
   }
}


Try and let me know.

and please do use a Breadboard.

Hope this helped.
_________________
while(!dead)
{
keepLearning();
}
temtronic



Joined: 01 Jul 2010
Posts: 9097
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Jul 18, 2019 10:05 am     Reply with quote

this....
Quote:
int time1=1000,time2=1000;

int in CCS defaults to 8 bits ( 0-255).....

Jay
dluu13



Joined: 28 Sep 2018
Posts: 395
Location: Toronto, ON

View user's profile Send private message Visit poster's website

PostPosted: Thu Jul 18, 2019 10:18 am     Reply with quote

temtronic wrote:
this....
int time1=1000,time2=1000;

int in CCS defaults to 8 bits ( 0-255).....

Jay


To add to that, you should read the CCS manual chapter "Data Definitions". In your case, if you want to use number greater than 255, then you must use "int16". Massam has captured this.

I suggest #include <stdint.h>, which contains many useful abbreviations for the different signed/unsigned datatypes
Luis123migueL



Joined: 17 Jul 2019
Posts: 6

View user's profile Send private message

PostPosted: Thu Jul 18, 2019 6:53 pm     Reply with quote

Hi everyone!!

Thank for your help.

Hi MassaM

I use breadboard now. I wrote your code with own function and it works very well . Also thanks to temtronic i notice by default int is equals to int8 and it just has 256 values from 0-255 (unsigned ) so i needed more than that for my code.

Now, i don´t undertand why you (MassaM) add this part in your code:

Code:

void __delay_ms(unsigned int16 time)
{
   while(time > 0)
   {
    delay_ms(1);
    time--;
   }
}


I do in breadboard the code below and it works well too, but I notice this code use 1% more ROM than yours, i don´t know if this is the goal of __delay_ms() function but both work very well:

Code:

#include<16f628.h>
#fuses XT,NOWDT
#use delay(clock=4000000)
#use standard_io(B)

void juego1(unsigned int16 a)
{
output_b(0b00000101);
delay_ms(a);
output_b(0b00001010);
delay_ms(a);
}
void juego2(unsigned int16 b)
{
output_b(0b00001001);
delay_ms(b);
output_b(0b00000110);
delay_ms(b);
}

void main()
{
unsigned int16 time1=1000,time2=1000;
output_low(pin_B0);
output_low(pin_B1);
output_low(pin_B2);
output_low(pin_B3);
While(true)
{
   juego1(time1);
   juego2(time2);
}
}



Thanks for your answers!!

Peace!!!
temtronic



Joined: 01 Jul 2010
Posts: 9097
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Jul 18, 2019 7:16 pm     Reply with quote

re: his delay function....

One possible reason...
The early versions of the compiler did not allow for 16bit values in the delay_ms(xxx) or delay_us(yyy).


Jay
Luis123migueL



Joined: 17 Jul 2019
Posts: 6

View user's profile Send private message

PostPosted: Thu Jul 18, 2019 8:26 pm     Reply with quote

Hi dluu13

I proved <stdint.h> header in breadboard and it works well, thanks for your advice.

Code:

#include<16f628.h>
#include <stdint.h>//for signed and unsigned variables
#fuses XT,NOWDT
#use delay(clock=4000000)
#use standard_io(B)


void __delay_ms(uint16_t time)
{
   while(time > 0)
   {
    delay_ms(1);
    time--;
   }
}

void juego1(uint16_t a)
{
output_b(0b00000101);
__delay_ms(a);
output_b(0b00001010);
__delay_ms(a);
}
void juego2(uint16_t b)
{
output_b(0b00001001);
__delay_ms(b);
output_b(0b00000110);
__delay_ms(b);
}

void main()
{
uint16_t time1=1000,time2=1000;
output_low(pin_B0);
output_low(pin_B1);
output_low(pin_B2);
output_low(pin_B3);
While(true)
{
   juego1(time1);
   juego2(time2);
}
}


Thanks a lot!!

Peace!!
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Thu Jul 18, 2019 11:35 pm     Reply with quote

As a 'general comment', get into the habit of including things after the
fuses and clock statement.
The order should be:

Processor include
fuses
clock
use setup for serial/I2C etc.
Then general includes
Then your code.

It doesn't matter in this case, but (for example), if you include functions
using serial I/O, they must be after the serial is defined. So it is worth
getting into the habit of using this order.
MassaM



Joined: 08 Jun 2019
Posts: 31

View user's profile Send private message Visit poster's website

PostPosted: Sun Jul 21, 2019 7:40 am     Reply with quote

temtronic wrote:
re: his delay function....

One possible reason...
The early versions of the compiler did not allow for 16bit values in the delay_ms(xxx) or delay_us(yyy).


Jay


Yes, this is the case. Also the case still on other compilers, so I made it a habit to always depend on this one.
_________________
while(!dead)
{
keepLearning();
}
MassaM



Joined: 08 Jun 2019
Posts: 31

View user's profile Send private message Visit poster's website

PostPosted: Sun Jul 21, 2019 7:47 am     Reply with quote

Luis123migueL wrote:
Hi everyone!!

Thank for your help.

Hi MassaM

I use breadboard now. I wrote your code with own function and it works very well . Also thanks to temtronic i notice by default int is equals to int8 and it just has 256 values from 0-255 (unsigned ) so i needed more than that for my code.

Now, i don´t undertand why you (MassaM) add this part in your code:

Code:

void __delay_ms(unsigned int16 time)
{
   while(time > 0)
   {
    delay_ms(1);
    time--;
   }
}


I do in breadboard the code below and it works well too, but I notice this code use 1% more ROM than yours, i don´t know if this is the goal of __delay_ms() function but both work very well:

Code:

#include<16f628.h>
#fuses XT,NOWDT
#use delay(clock=4000000)
#use standard_io(B)

void juego1(unsigned int16 a)
{
output_b(0b00000101);
delay_ms(a);
output_b(0b00001010);
delay_ms(a);
}
void juego2(unsigned int16 b)
{
output_b(0b00001001);
delay_ms(b);
output_b(0b00000110);
delay_ms(b);
}

void main()
{
unsigned int16 time1=1000,time2=1000;
output_low(pin_B0);
output_low(pin_B1);
output_low(pin_B2);
output_low(pin_B3);
While(true)
{
   juego1(time1);
   juego2(time2);
}
}



Thanks for your answers!!

Peace!!!


You are most welcome and glad it helped. Very Happy

Yes, breadboard-ing not only does actually kicka** simulation over and over again, but it also helps you understand the whole deal together, both hardware and software. Total DIY with heart! Cool

I'd rather DIY than ready made boards/shields for beginner to intermediate stuff.

Thank you and Keep Learning! Very Happy
_________________
while(!dead)
{
keepLearning();
}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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