Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Please help me on CCS C for PIC.

Status
Not open for further replies.

wonbinbk

Junior Member level 2
Joined
Dec 21, 2004
Messages
23
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,281
Activity points
266
setup_adc trong ccs

I am the beginner in CCS.
I use PID controller to control the velocity of a DC motor.
Here is my code. But it doesn't work. Please help me 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);

}
 

ccs c encoder to velocity code

Can you post Sch and write English Comment ?
 

project in ccs how to use

Do a search on pid at: https://www.ccsinfo.com/forum/

Lots of information there, or submit your questions there. They are very helpfull.
 

pwm trong ccs

wonbinbk,
I believe that your problem are #fuses from uC, They are not defined.
Otherwise, the freq is too much.
Can you send me your project to help you?

Best Regards
 

pwm pic ccs

U get help dont post the code and aske for help...

no code works directly .. u need to research and dig into the code if not working.

-helios
 

create new project for pic in ccs

FSio said:
wonbinbk,
I believe that your problem are #fuses from uC, They are not defined.
Otherwise, the freq is too much.
Can you send me your project to help you?

Best Regards
Dear FSio!
Thank you for your advice. I will send you my project on English. Hope it useful for you.
My code is wrong. Thank for all your advies.
yeah! I wrote this code too hurriedly.
I test with CCS on the simplest program to check if i could write CCS. And I have a problem on PWM. ADC, I/O ... is Ok! Please help me!
I use CCS to compile, PICsimulatorIDE to simulate it after compiled.
Here the code:
// PIC16F88
// PWM output on RB3. This is the default. I don't know how to change PWM output to RB0.
#include <16F88.h>
#use delay(clock=4000000)
#fuses NOWDT,XT //Thanks FSio!

void main()
{
Setup_CCP1(CCP_PWM);
Setup_Timer_2(T2_DIV_BY_4, 199,1) ; // I want the period is 800us.
Set_PWM1_duty(400); // with duty=400us.
Loop:
Goto loop; //I know this is a weird loop. But If I use the While(1), PICsimu
// not run "loop", just do some instructions, then stop.
}
But when I use PICsimu, and an Oscilo (PICsimu) on RB3, I don't see anything happen except the ray go on "0" logic. Hic! Help me!
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top