Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

I need help on my CCS code

Status
Not open for further replies.

wonbinbk

New Member
Hi all! I am the beginner in CCS.
I use the PID controller to control the velocity of the DC motor.
Here is my code, it does not work. And I don't know why,
Please help out.

#include "16F88.h"
#USE standard_io(B)
#use delay(clock=4000000)
double xung , Kp_t, Ki_t, Kd_t,Kp,Ki,Kd ,v_set, v_cur, e2, e1, e_sum, e_del, duty ;
#int_TIMER1
TIMER1_isr()
{
xung = get_timer0(); // xung= so xung dem duoc trong 1 ms
set_timer0 (0);
set_timer1 (-1000);
}

main()
{

setup_adc_ports(sAN0|sAN1|sAN2|sAN3|VSS_VDD);
setup_adc(ADC_CLOCK_DIV_8);

setup_timer_0(RTCC_EXT_L_TO_H|RTCC_DIV_1);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
setup_timer_2(T2_DIV_BY_1,255,1); /*Chu ki cua PWM= (255 +1)* 4* 1us * (T2DIV)= 1024us
Do do tan so cua PWM la 976Hz
Muon tang gia tri chu ki PWM len, thi cao lam cung chi la
1024*16=16384 us<-->61 Hz.*/
setup_ccp1(CCP_PWM);


enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
/*****************************************************************/
// NOI NHAP HE SO PID CHO HE THONG KHI DIEU KHIEN TU DONG.
Kp_t=10;
Ki_t= 1;
Kd_t=0.1;
duty= 100;// 100 micro giay duty cycle.
/******************************************************************/
do{
/****** Quay phai hay quay trai?**************************/
If (PIN_B0 == 0)
output_low (PIN_B2) ; //Quay trai
Else output_high (PIN_B2); //quay phai.

/*********************************************************/
If (input(PIN_B5)==0) // PID tu dong
{ Kp = Kp_t;
Ki = Ki_t;
Kd = Kd_t;
}
Else //PID bang tay, chinh bang bien tro.
// Kp, Ki, Kd = 0--> 100
// 1 buoc adc = 100/1024= 0.0976
{set_adc_channel (1); //lay Kp
delay_us (500); //cho 500 us cho adc chinh xac.
Kp= read_adc()*0.0976; //Kp=gia tri tu bien tro.
set_adc_channel(2);
delay_us (500);
Ki=read_adc()*0.0976;
set_adc_channel (3);
delay_us (500);
Kd=read_adc()*0.0976;
}

/*************************************************************/
// lay van toc can thiet tu bien tro AN0, van toc = 0 --> 300 vong/phut
set_adc_channel(0);
delay_us(500);
v_set= read_adc()* 0.293; // read_adc = 0--> 1023
// 0.293= van toc toi da (300 v/p) / 1024

/******************************************************************************/
// lay van toc tuc thoi tu encoder.
// encoder 200 xung/vong,
//so vong trong 1phut = so xung trong 1ms * 12000000

set_timer0 (0); // dem xung encoder dua ve
set_timer1 (-1000); // thoi gian lay mau doi voi encoder la 1000us
v_cur= xung *12000000; // van toc hien tai (vo`ng/ phu't)

/******************************************************************************/
/******************************************************************************/


e2= v_set- v_cur ;
e_sum= e2 + e1;
e_del= e2 - e1;
e1=e2;
duty= duty + Kp*e2 + Ki*e_sum + Kd*e_del; // Day la he thuc tinh PID roi rac.
set_pwm1_duty(610);
}
while ( true);

}
 
Here are some steps, and questions to answer, when trying to solve a problem:

1) What specifically isn't working? "The car doesn't work" is too vague and isn't helpful to anyone. "The car doesn't start" is a better description of the problem.
2) What are the results and what are the conditions? I.e. "The key turns and it goes click-click," or "The engine turns, but it still doesn't start".
3) Are there any conditions under which it will work?
4) Break the task into smaller pieces and build up. Try to get one part to work and then add parts until it doesn't work. Assembling a car without testing any subcomponent and expecting it to work is just foolish.

Mike
 
What I don't understand is why the following line takes a -ve value?
set_timer1 (-1000);
In the CCS manual it says that all timer's count up! (p.159 July 2005 manual) So how would it count up to -1000?

Should that even compile?


Also, I haven't been programming with CCS in a while but shouldn't the If-Else statement be written as if-else? Note capital letters. Can somebody else confirm this?


/****** Quay phai hay quay trai?**************************/
If (PIN_B0 == 0)
output_low (PIN_B2) ; //Quay trai
Else output_high (PIN_B2); //quay phai.

Anyway like the guys said, you need to give us more info.
 
pop said:
What I don't understand is why the following line takes a -ve value?
set_timer1 (-1000);
In the CCS manual it says that all timer's count up! (p.159 July 2005 manual) So how would it count up to -1000?

Should that even compile?


Also, I haven't been programming with CCS in a while but shouldn't the If-Else statement be written as if-else? Note capital letters. Can somebody else confirm this?


/****** Quay phai hay quay trai?**************************/
If (PIN_B0 == 0)
output_low (PIN_B2) ; //Quay trai
Else output_high (PIN_B2); //quay phai.

Anyway like the guys said, you need to give us more info.

Thank all for your advances.
I started to write C code to do ADC, PWM,I/O...
And I have another problem. Please help me:
This is my code and the "sche"
// PIC=16F88
// I want to output PWM on RB3. (How to make it RB0?)
// I read EXPWM.c , an example of CCS
// I use PICsimu to check for the pulse on RB3 by its Ocscilo, but don't
// see anything!
#include <16F88.h>
#use delay(clock=4000000)
#fuses NOWDT,XT
void main()
{
SETUP_CCP1(CCP_PWM);
SETUP_TIMER_2(T2_DIV_BY_4,199,1);
SET_PWM1_DUTY(400); //PWM with Period=800us, duty=400us.
loop:
GOTO loop; //weird loop! I know that. But if use While(1), PICsimu
//will run just some instructions, then it stop.
}

Please help me out, I think it is the fault of PICsimu.
If not, then there must be a "button" which I must switch it on before PWM run. Because I see Timer2 register increase every cycle. But no pulse RB3. Hic!
Pleas help me!
 
please use the CODE tag when you give us example code... it preserves the formatting so it's more readable.

it's as easy as clicking the "code" button, pasting your code, and clicking the "code" button again.
 
I am really sorry.
I post the code again here, please help me.
Code:
// PIC=16F88 
// I want to output PWM on RB3. (How to make it RB0?) 
// I read EXPWM.c , an example of CCS 
// I use PICsimu to check for the pulse on RB3 by its Ocscilo, but don't 
// see anything! 
#include <16F88.h> 
#use delay(clock=4000000) 
#fuses NOWDT,XT 
void main() 
{ 
SETUP_CCP1(CCP_PWM); 
SETUP_TIMER_2(T2_DIV_BY_4,199,1); 
SET_PWM1_DUTY(400); //PWM with Period=800us, duty=400us. 
loop: 
GOTO loop; //weird loop! I know that. But if use While(1), PICsimu 
//will run just some instructions, then it stop. 
}
[/code]
 
wonbinbk said:
I am really sorry.
I post the code again here, please help me.
Code:
// PIC=16F88 
// I want to output PWM on RB3. (How to make it RB0?) 
// I read EXPWM.c , an example of CCS 
// I use PICsimu to check for the pulse on RB3 by its Ocscilo, but don't 
// see anything! 
#include <16F88.h> 
#use delay(clock=4000000) 
#fuses NOWDT,XT 
void main() 
{ 
SETUP_CCP1(CCP_PWM); 
SETUP_TIMER_2(T2_DIV_BY_4,199,1); 
SET_PWM1_DUTY(400); //PWM with Period=800us, duty=400us. 
loop: 
GOTO loop; //weird loop! I know that. But if use While(1), PICsimu 
//will run just some instructions, then it stop. 
}
[/code]
Did you know you have to clear CCPMX configuration bit in order to switch PWM feature from RB0 to RB3 ?
 
Jay.slovak said:
wonbinbk said:
I am really sorry.
I post the code again here, please help me.
Code:
// PIC=16F88 
// I want to output PWM on RB3. (How to make it RB0?) 
// I read EXPWM.c , an example of CCS 
// I use PICsimu to check for the pulse on RB3 by its Ocscilo, but don't 
// see anything! 
#include <16F88.h> 
#use delay(clock=4000000) 
#fuses NOWDT,XT 
void main() 
{ 
SETUP_CCP1(CCP_PWM); 
SETUP_TIMER_2(T2_DIV_BY_4,199,1); 
SET_PWM1_DUTY(400); //PWM with Period=800us, duty=400us. 
loop: 
GOTO loop; //weird loop! I know that. But if use While(1), PICsimu 
//will run just some instructions, then it stop. 
}
[/code]
Did you know you have to clear CCPMX configuration bit in order to switch PWM feature from RB0 to RB3 ?
Yeah! I know it, exactly in the datasheet. But I wonder if there is a "CCS" way to configure it? or we have to set (or clear) one bit, with exactly bit address?
But now, I know that CCSC allow us to map every bit that it can... So, thank you for helping me. Hope you all happy!
 
Ah! By the way, I have simulate my PWM program. It works. And the code is no different from this code I have posted here. The reason is my PICsimulatorIDE had an error on PWM simu. Thanks all!
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top