Correction for my code ! - Page 1
Forum section: electronics / general

author: e-thought
author_rank: Contributor
timestamp: April 13, 2015, 08:55:24 am
content: 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 #1Measuring the Period of aSquare WaveFigure 1-1: PeriodTt1t21.Configure control bits CCPxM3:CCPxM0(CCPxCON<3:0>) to capture every risingedge of the waveform.2.Configure the Timer1 prescaler so Timer1with run Tm a x(1)without overflowing.3.Enable the CCP interrupt (CCPxIE bit).4.When a CCP interrupt occurs:a)Subtract saved captured time (t1) fromcaptured time (t2) and store (use Timer1interrupt 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 edgeINTCON.PEIE=1;PIE1.CCP1IE=1;// enable ccp1 interruptpie1.tmr1ie=1 ; //enable tmr1 overflow interr//tmr1config:t1con.t1ckps= ?;t1con.tmr1cs=0;//fosc/4t1con.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

author: ctz
author_rank: Contributor
timestamp: April 13, 2015, 09:15:58 am
content: 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.

author: e-thought
author_rank: Contributor
timestamp: April 13, 2015, 09:21:41 am
content: 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 : )

author: andtfoot
author_rank: Unknown
timestamp: April 13, 2015, 09:36:01 am
content: 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.

author: e-thought
author_rank: Contributor
timestamp: April 13, 2015, 09:42:59 am
content: 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

author: Mechatrommer
author_rank: Super Contributor
timestamp: April 13, 2015, 10:30:44 am
content: Quote from: e-thought on April 13, 2015, 09:21:41 amas 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 thisCode: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: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?

author: e-thought
author_rank: Contributor
timestamp: April 13, 2015, 12:55:15 pm
content: 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_InitPWM1_Set_DutyPWM1_StartPWM1_Stopi 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

author: Mechatrommer
author_rank: Super Contributor
timestamp: April 13, 2015, 05:50:54 pm
content: well i got many mails from nigeria or somewhere want to make friend, they even want to bank transfer me million dollar.

author: e-thought
author_rank: Contributor
timestamp: April 13, 2015, 06:09:44 pm
content: 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 : )