|
|
View previous topic :: View next topic |
Author |
Message |
goizb0307
Joined: 03 Jul 2015 Posts: 2
|
CCS compiler Code |
Posted: Fri Jul 03, 2015 3:26 am |
|
|
Can anyone roughly explain about the coding below to me a. Use PIC16F877A. Thanks in advance.
Code: |
#include <16F877A.h>
#device adc=10
#use delay(clock=20000000)
#define XTAL_FREQ 200000000
#include <flex_lcd.c>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#FUSES HS, NOWDT,NOPROTECT,NOLVP,NOWDT,PUT,NOBROWNOUT
#define MAX_VALUE 200
#define CCW_ROTATION MAX_VALUE - 25
#define CW_ROTATION MAX_VALUE - 7
#define STOP_ROTATION MAX_VALUE
#define THRESHOLD_VALUE 50
#define A0 PIN_A0
#define A1 PIN_A1
#define B1 PIN_B1
unsigned char pulse_max=0;
unsigned char pulse_top=0;
unsigned char top_value=0 ;
#INT_TIMER0
static void TIMER0_isr(void)
{
if(INT_TIMER0)// TIMER0 Interrupt Flag
{
pulse_max++; // Pulse Max Increment
pulse_top++; // Pulse Top Increment
/* MAX_VALUE=200 turn off the pulse */
if (pulse_max >= MAX_VALUE) {
pulse_max=0;
pulse_top=0;
output_low(B1); // Turn off RB1 //calculate pulse
}
if (pulse_top == top_value) {
output_high(B1); // Turn On RB1
}
}
}
void main(void)
{
|
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9225 Location: Greensville,Ontario
|
|
Posted: Fri Jul 03, 2015 5:07 am |
|
|
1) it uses a near obsolete PIC the 877.....
2) there is no MAIN() program....
3) several libraries are included but not used...
4) the PIC will run at the max speed and the ADC is set for 10 bits
5) supposed to have an LCD module but again no MAIN() so no idea WHY the LCD code
6) uses TIMER0 for some sort of 'pulse width' calculation though ISr is NOT enabled, again NOT a complete program.
My 'guess' is that it's a 'decoder' for a hobby RC servo unit???? It _might_ replace an RC servo, decode the data stream and then control an LED ?
Without the complete program it's hard to decide WHAT the programmer had in mind...
Jay |
|
|
goizb0307
Joined: 03 Jul 2015 Posts: 2
|
|
Posted: Fri Jul 03, 2015 9:05 am |
|
|
temtronic wrote: | 1) it uses a near obsolete PIC the 877.....
2) there is no MAIN() program....
3) several libraries are included but not used...
4) the PIC will run at the max speed and the ADC is set for 10 bits
5) supposed to have an LCD module but again no MAIN() so no idea WHY the LCD code
6) uses TIMER0 for some sort of 'pulse width' calculation though ISr is NOT enabled, again NOT a complete program.
My 'guess' is that it's a 'decoder' for a hobby RC servo unit???? It _might_ replace an RC servo, decode the data stream and then control an LED ?
Without the complete program it's hard to decide WHAT the programmer had in mind...
Jay |
Actually this coding is for multiple axis solar tracker. Below is the complete code.
Code: |
#include <16F877A.h>
#device adc=10
#use delay(clock=20000000)
#define XTAL_FREQ 200000000
#include <flex_lcd.c>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#FUSES HS, NOWDT,NOPROTECT,NOLVP,NOWDT,PUT,NOBROWNOUT
#define MAX_VALUE 200
#define CCW_ROTATION MAX_VALUE - 25
#define CW_ROTATION MAX_VALUE - 7
#define STOP_ROTATION MAX_VALUE
#define THRESHOLD_VALUE 50
#define A0 PIN_A0
#define A1 PIN_A1
#define B1 PIN_B1
unsigned char pulse_max=0;
unsigned char pulse_top=0;
unsigned char top_value=0 ;
#INT_TIMER0
static void TIMER0_isr(void)
{
if(INT_TIMER0)// TIMER0 Interrupt Flag
{
pulse_max++; // Pulse Max Increment
pulse_top++; // Pulse Top Increment
/* MAX_VALUE=200 turn off the pulse */
if (pulse_max >= MAX_VALUE) {
pulse_max=0;
pulse_top=0;
output_low(B1); // Turn off RB1 //calculate pulse
}
if (pulse_top == top_value) {
output_high(B1); // Turn On RB1
}
}
}
void main(void)
{
signed int16 ldr_left=0 ;
signed int16 ldr_right=0 ;
signed int16 ldr_diff ;
signed int16 lf;
signed int16 lr;
lcd_init();
port_b_pullups(TRUE);
setup_adc_ports(AN0_AN1_AN3);
setup_adc(ADC_CLOCK_DIV_32);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_2);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
// Init Servo Pulse
pulse_max=0;
pulse_top=0;
top_value = MAX_VALUE; // top_value = MAX_VALUE: Servo Motor Stop
// Init TIMER0: Period: Fosc/4 x Prescaler x TMR0
enable_interrupts(INT_TIMER0);
enable_interrupts(GLOBAL);
// Read the ADC here
for(;;){
//1 for ADC result to be right-justified, 0 for left-justified
set_adc_channel(0);
delay_us(10);
ldr_left=read_adc();
set_adc_channel(1);
delay_us(10);
ldr_right=read_adc();
delay_ms(10);
lf = ((ldr_left*5)/1024);
printf(lcd_putc,"\f \%ld \n",lf);
lr=((ldr_right*5)/1024);
printf(lcd_putc," \%ld",lr);
delay_ms(20);
// Get the different//
ldr_diff = ldr_left - ldr_right;
if ((ldr_diff >= -THRESHOLD_VALUE) && (ldr_diff <= THRESHOLD_VALUE))
{
top_value = MAX_VALUE;; // Stop the Servo Motor
}
else
{
if (ldr_diff > THRESHOLD_VALUE)
{
top_value = CCW_ROTATION; // Counterclockwise Rotation
}
else
{
top_value = CW_ROTATION; // Clockwise Rotation
}
}
}
} |
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9225 Location: Greensville,Ontario
|
|
Posted: Fri Jul 03, 2015 10:35 am |
|
|
I don't see how this is a multi -axis solar controller. It only reads 2 photocells ( ldr_left, ldr_right), computes which side is brighter and moves the servo to get max input.
The sparse comments do kinda say what is happening so what do you need to know?
Jay |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1933 Location: Norman, OK
|
|
Posted: Fri Jul 03, 2015 11:10 am |
|
|
temtronic,
You are correct, this is not a multi-axis project, just basic servo control.
Granted it would make a good start for a multi-axis solar controller...
It appears the project was converted from original MikroC and is found here:
www.uprb.edu/profesor/jramon/basicservomotorcontrol.pdf
goizb0307
The project is very well explained in the PDF at the above link and there is no
need for us to explain the entire thing here. After you read it, if you have
specific questions about the CCS code portions ask away... _________________ Google and Forum Search are some of your best tools!!!! |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|