Author Topic: Correction for my code !  (Read 3209 times)

0 Members and 1 Guest are viewing this topic.

Offline e-thoughtTopic starter

  • Contributor
  • Posts: 30
  • Country: 00
  • if you live twice, then you should think twice too
Correction for my code !
« on: April 13, 2015, 08:55:24 am »
Question 1: what timer use the delay function, because when i use tmr0 overflow with delay_ms() , it generates an error, wich says not enough memory .


Question2: i want to do this tip to measure the tiùme between a transmitted and received ultrasonic wave for  telemeter application:
this is the tip :
------------------------------------------
TIP #1
Measuring the Period of a
Square Wave
Figure 1-1: Period
T
t1
t2
1.
Configure control bits CCPxM3:CCPxM0
(CCPxCON<3:0>) to capture every rising
edge of the waveform.
2.
Configure the Timer1 prescaler so Timer1
with run T
m a x
(1)
without overflowing.
3.
Enable the CCP interrupt (CCPxIE bit).
4.
When a CCP interrupt occurs:
a)
Subtract saved captured time (t1) from
captured time (t2) and store (use Timer1
interrupt flag as overflow indicator).
b)
Save captured time (t2).
c)
Clear Timer1 flag if set.
The result obtained in step 4.a is the period (T)
------------------------
Does this code translate the instruction above:(i'm working with mikroc )
 
void  interrupt()
{
if(pir1.ccp1if==1)
{
  t1=tmr1:l;
  t2=tmr1:l +tmr1:h;
T=t2-t1;
pir1.ccp1if=0;

 }
}
void main()
{
CCP1CON.CC1M<3:0>=0100;   // every rising edge
INTCON.PEIE=1;
PIE1.CCP1IE=1;// enable ccp1 interrupt
pie1.tmr1ie=1 ; //enable tmr1 overflow interr
//tmr1config:
t1con.t1ckps= ?;
t1con.tmr1cs=0;//fosc/4
t1con.tmr1on=1; /enable tmr1
}

please tell if i've done all the step described, and correct me if 'im wrong, i much appreciate this forum, thank you very much !(i promise my code will be available for this community once i finish it ! what's more good than sharing the knowledje :p
« Last Edit: April 13, 2015, 09:41:46 am by e-thought »
God, i'm begging your mercy !
 

Offline ctz

  • Contributor
  • Posts: 26
  • Country: gb
Re: Correction for my code !
« Reply #1 on: April 13, 2015, 09:15:58 am »
Turn on all available warnings. The very first line should produce the following warning:

"warning: suggest parentheses around assignment used as truth value"

Which should give you enough of a clue to your typo to fix it yourself.
 

Offline e-thoughtTopic starter

  • Contributor
  • Posts: 30
  • Country: 00
  • if you live twice, then you should think twice too
Re: Correction for my code !
« Reply #2 on: April 13, 2015, 09:21:41 am »
I've found too much error, but don't know how to correct them, i'm working with pic16f690 please need help.

@Ctz: didn't understood your answer, if you can tell me more : )
God, i'm begging your mercy !
 

Offline andtfoot

  • Supporter
  • ****
  • Posts: 352
  • Country: au
Re: Correction for my code !
« Reply #3 on: April 13, 2015, 09:36:01 am »
A couple of things off the bat for question 2, purely on syntax (assuming my C isn't too rusty):
- Use '==' for comparing values and '=' for assigning values. What are you trying to do in your IF statement?
- Each section of code (e.g. interrupt() and main() ) needs brackets {} around their contents.

I'm not sure about the usage of :  in C code for variable names, but maybe I've just never come across it before.
 

Offline e-thoughtTopic starter

  • Contributor
  • Posts: 30
  • Country: 00
  • if you live twice, then you should think twice too
Re: Correction for my code !
« Reply #4 on: April 13, 2015, 09:42:59 am »
lol so true, because i'm beginner i omitted these things,  now i did the correction, and i need the correction for the procedure also, if it fits with the tip or not ? thank you very much
God, i'm begging your mercy !
 

Offline Mechatrommer

  • Super Contributor
  • ***
  • Posts: 11643
  • Country: my
  • reassessing directives...
Re: Correction for my code !
« Reply #5 on: April 13, 2015, 10:30:44 am »
I've found too much error, but don't know how to correct them, i'm working with pic16f690 please need help.
as i'm working with the pic16f690 right now too, maybe you are in luck.... 1st you setup timer prescaler and any other parameters BEFORE enabling interrupt. 2nd what you are trying to achieve? i saw you try to enable both overflow (TMR1IE) and capture (CCP1IE) interrupt at the same time for what? and then your interrupt routine only handle capture interrupt (CCP1IF) only, then what is this? and 3rd this
Code: [Select]
t1=tmr1:l;
t2=tmr1:l +tmr1:h;
T=t2-t1;
that is nonsense from the purpose you described, measure flight travel? you sample LSB in t1 and subtract from t2 (full 16bits value) of the same sampled time. its like removing remainder that is not divisible by 256, is there something you didnt tell us? from my view your code doing nothing you dont even provide infinite loop your code will just be lost in oblivion.
Code: [Select]
t1con.t1ckps= ?;what is this? you try to tell the code to make coffee for you?... so... feeling lucky? ;)

edition:
you must also set what type of capture you are up to, CCP1CON:CCP1M<3:0> i think its b'0101' capture on rising edge... page 127 from PIC16F631-677-685-687-689-690.pdf download it if you havent. and then you havent setup CCP1 pin as input or whatever to prepare for the capture.

hint. measuring flight travel, you save t1 earlier just before you emit your radioactive, wait whats that... yeah... just before you emit your ultrasonic wave, not during interrupt detection. t2 captured when arrival is detected just like you coded there, then voila subtract and multiply by prescaler or somesort to get time travel. but where in your code to transmit the wave?  :scared:
« Last Edit: April 13, 2015, 10:55:37 am by Mechatrommer »
Nature: Evolution and the Illusion of Randomness (Stephen L. Talbott): Its now indisputable that... organisms “expertise” contextualizes its genome, and its nonsense to say that these powers are under the control of the genome being contextualized - Barbara McClintock
 

Offline e-thoughtTopic starter

  • Contributor
  • Posts: 30
  • Country: 00
  • if you live twice, then you should think twice too
Re: Correction for my code !
« Reply #6 on: April 13, 2015, 12:55:15 pm »
Thank you very much man, this is a very good reply, yes i missed some parameters like setting ccp1 as input. the pwm thing is generated with simple 4 commands that are : PWM1_Init
PWM1_Set_Duty
PWM1_Start
PWM1_Stop

i did, while () and then youu include your command, a simple thing, just tape it on the help to get it..
-t1con.t1ckps= ? ==> because i didn't calculate the prescalar suitable to my application, so i just mentionned that there is a prescalar bit that must bbbe configured.
 
-t1=tmr1:l;
t2=tmr1:l +tmr1:h;
T=t2-t1;
this part of the program i don't really understand it, if you can explain to me how to assign the value of the timer at some point, because i just copied it from somewhere but not really understand how it works.

-About the ccp1m<3:0>: i'm right : ) it's the correct config.

i'll edit the code, and share it here, maybee this afternoon.

P.-s: i'm muslim too and i'm from algeria, if you want us to be freind, no matter  :p


God, i'm begging your mercy !
 

Offline Mechatrommer

  • Super Contributor
  • ***
  • Posts: 11643
  • Country: my
  • reassessing directives...
Re: Correction for my code !
« Reply #7 on: April 13, 2015, 05:50:54 pm »
well i got many mails from nigeria or somewhere want to make friend, they even want to bank transfer me million dollar.
Nature: Evolution and the Illusion of Randomness (Stephen L. Talbott): Its now indisputable that... organisms “expertise” contextualizes its genome, and its nonsense to say that these powers are under the control of the genome being contextualized - Barbara McClintock
 

Offline e-thoughtTopic starter

  • Contributor
  • Posts: 30
  • Country: 00
  • if you live twice, then you should think twice too
Re: Correction for my code !
« Reply #8 on: April 13, 2015, 06:09:44 pm »
hAhaa these guys from africa want to have the money of all the world, lol, i'm not from nigeria, i'm from algeria, it's on the medeteranian sea. search it on google it's so beautifull country : )
God, i'm begging your mercy !
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf