View previous topic :: View next topic |
Author |
Message |
Luis123migueL
Joined: 17 Jul 2019 Posts: 6
|
blinking led - my code does not work |
Posted: Thu Jul 18, 2019 12:12 am |
|
|
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
|
|
Posted: Thu Jul 18, 2019 6:32 am |
|
|
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!
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: 9221 Location: Greensville,Ontario
|
|
Posted: Thu Jul 18, 2019 10:05 am |
|
|
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
|
|
Posted: Thu Jul 18, 2019 10:18 am |
|
|
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
|
|
Posted: Thu Jul 18, 2019 6:53 pm |
|
|
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: 9221 Location: Greensville,Ontario
|
|
Posted: Thu Jul 18, 2019 7:16 pm |
|
|
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
|
|
Posted: Thu Jul 18, 2019 8:26 pm |
|
|
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: 19496
|
|
Posted: Thu Jul 18, 2019 11:35 pm |
|
|
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
|
|
Posted: Sun Jul 21, 2019 7:40 am |
|
|
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
|
|
Posted: Sun Jul 21, 2019 7:47 am |
|
|
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.
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!
I'd rather DIY than ready made boards/shields for beginner to intermediate stuff.
Thank you and Keep Learning! _________________ while(!dead)
{
keepLearning();
} |
|
|
|