CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Servo tester

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
thaking



Joined: 22 Jan 2011
Posts: 25

View user's profile Send private message

Servo tester
PostPosted: Fri Mar 11, 2011 12:10 pm     Reply with quote

Can anyone please help me with "translating" code from Basic into CCS
Code:

Device 16F877A
Config HS_OSC, WDT_OFF, PWRTE_OFF, LVP_OFF
ALL_DIGITAL TRUE
XTAL = 10

;LCD
Declare LCD_TYPE ALPHA
Declare LCD_LINES 4
Declare LCD_INTERFACE 4
Declare LCD_RSPIN PORTD.0
Declare LCD_ENPIN PORTD.1
Declare LCD_DTPIN PORTD.4

;ADC
;NOTE: the ADC will convert any input Raw between 0-5 V to a digital count between 0-1023 !!
Declare ADIN_RES 10       ;10-bit result required
Declare ADIN_TAD FRC      ;Sampling rate
Declare ADIN_STIME 50     ;Allow 50us sample time
ADCON1 = %10001110                                     ;PORTA.0 (AN0) will be an analog ('A') input.  All other PORTA ports will be digital I/O ('D').
                                                       ;bit7: 1=Right justified 0=Left justified
                                                       ;bits0-3: Specify pins to be used for A/D conversion.  See PCF16F877A datasheet for more info.

;Global configuration
;ALL_DIGITAL TRUE              ;All inputs digital
;PORTB_PULLUPS On             ;Enable internal pullup resistors (no longer required as I am using PORTA for inputs)
TRISA = %11111111             ;0=output, 1=input.  See 'Declare Symbols' for details.
TRISD = %00000000             ;PORTD is output for LCD display
Clear                         ;Erase RAM memory

;Declare Variables
Dim Raw              As Word   ;This variable contains a value from 0 to 1023 as digitized voltmeter value.
Dim Quanta           As Float  ;This variable contains a quantisizing value
Dim Volts            As Float  ;This variable contains a value from 0 to 5 to reflect the measured voltage.
Dim Position         As Word   ;This variable contains a value from 1000 to 2000 to drive the servo.
Dim Steps            As Word   ;This variable contains a value between 0 and 50 to control the sweep speed.
Dim Menu             As Byte   ;This variable indicates the active menu.

;Declare global Symbols
Symbol Out          = PORTD.3    ;Port used to connect the servo.
Symbol BtnPrev      = PORTA.2    ;Port used for the 'Previous' pushbutton (CURRENTLY NOT USED)
Symbol BtnNext      = PORTA.3    ;Port used For the 'Next' pushbutton

GoTo Start

;Subroutines

Servo_center:
  Servo Out, 1500    ;Send a pulse of 1,5mSec (=1500uSec) to servo (this will center the servo)
  DelayMS 20         ;This will result in the pulse being transmitted 50x per second
Return

Servo_adc:
  Raw = ADIn 0                                     ;Place the conversion into variable Raw
                                                   ;The ADC will convert any input voltage between 0-5 V to a digital count between 0-1023
  Quanta = 5.0 / 1024                              ;Calculate the quantising value
  Volts = Raw * Quanta                             ;Quantisize the measured data
  Position = 1000 + 200* Volts                     ;Voltage=0.00;Position=1000us;servo=min  Voltage=5.00;Position=2000us;servo=max
  Print At 1, 1, "Napetost  = ", DEC2 Volts, "V"    ;Put measured voltage on LCD display (0-5)
  Print At 2, 1, "Pozicija= ", Dec Position, "  " ;Put calculated value on LCD display (1000 to 2000)
  Servo Out, Position                              ;Send 'Position' pulses to servo
  DelayMS 20                                       ;20mSec delay = 50Hz frequency repeat of pulse transmission to servo.
Return

Servo_loop_slow:
  Print At 2, 1, "1ms -> 2ms /20us"
  For Position = 1000 To 2000 Step 20  ;Increase with steps of 20uSec
    Servo Out, Position                ;Send 'Position' pulses to servo
    DelayMS 20                         ;20mSec delay = 50Hz frequency repeat of pulse transmission to servo.
  Next
  Print At 2, 1, "1ms <- 2ms /20us"
  For Position = 2000 To 1000 Step -20 ;Decrease with steps of 20uSec
    Servo Out, Position                ;Send 'Position' pulses to servo
    DelayMS 20                         ;20mSec delay = 50Hz frequency repeat of pulse transmission to servo.
  Next
Return

Servo_loop_fast:
  Print At 2, 1, "1ms -> 2ms /50us"
  For Position = 1000 To 2000 Step 50  ;Increase with steps of 50uSec
    Servo Out, Position                ;Send 'Position' pulses to servo
    DelayMS 20                         ;20mSec delay = 50Hz frequency repeat of pulse transmission to servo. 
  Next
  Print At 2, 1, "1ms <- 2ms /50us"
  For Position = 2000 To 1000 Step -50 ;Decrease with steps of 50uSec
    Servo Out, Position                ;Send 'Position' pulses to servo
    DelayMS 20                         ;20mSec delay = 50Hz frequency repeat of pulse transmission to servo.
  Next
Return

Servo_loop_variable_speed:
  Raw = ADIn 0                                     ;Place the conversion into variable Raw
                                                   ;The ADC will convert any input voltage between 0-5 V to a digital count between 0-1023
  Quanta = 90.0 / 1024                             ;Calculate the quantising value.  This will modify the range values from 0-1023 to 0-90 instead.
  Steps = Raw * Quanta                             ;Quantisize the measured data
  Steps = Steps + 10.0                             ;Add a value of 10 to offset the minimum value to 10 instead of 0.
                                                   ;The range values will now go from 10 to 100.
  Print At 2, 1, "1ms -> 2ms /",DEC2 Steps,"us"
  For Position = 1000 To 2000 Step Steps           ;Increase
    Servo Out, Position                            ;Send 'Position' pulses to servo
    DelayMS 20                                     ;20mSec delay = 50Hz frequency repeat of pulse transmission to servo. 
  Next
  Print At 2, 1, "1ms <- 2ms /",DEC2 Steps,"us"
  For Position = 2000 To 1000 Step -Steps          ;Decrease
    Servo Out, Position                            ;Send 'Position' pulses to servo
    DelayMS 20                                     ;20mSec delay = 50Hz frequency repeat of pulse transmission to servo.
  Next
Return


Menu0:
Cls
Print At 1, 1, "Center Servo    "
Print At 2, 1, "1500us          "
While 1 = 1
    GoSub Servo_center
    If BtnNext = 0 Then
        DelayMS 25                  ;Small delay in case pushbutton was not pressed down hard enough.
        While BtnNext = 0 : Wend    ;Wait until pushbutton is released.
        Menu = 1 ;Next
        GoTo Loop
    EndIf
   
;    If BtnPrev = 0 Then
;        DelayMS 25                 ;Small delay in case pushbutton was not pressed down hard enough.
;        While BtnPrev = 0 : Wend   ;Wait until pushbutton is released.
;        Menu = 2 ;Next
;        GoTo Loop
;    EndIf
Wend


Menu1:
Cls
Print At 1, 1, "Manual mode        "
While 1 = 1
    GoSub Servo_adc
    If BtnNext = 0 Then
        DelayMS 25                  ;Small delay in case pushbutton was not pressed down hard enough.
        While BtnNext = 0 : Wend    ;Wait until pushbutton is released.
        Menu = 2 ;Next
        GoTo Loop
    EndIf
   
;    If BtnPrev = 0 Then
;        DelayMS 25                  ;Small delay in case pushbutton was not pressed down hard enough.
;        While BtnPrev = 0 : Wend    ;Wait until pushbutton is released.
;        Menu = 0 ;Next
;        GoTo Loop
;    EndIf
Wend


Menu2:
Cls
Print At 1, 1, "Servo Fast    "
While 1 = 1
    GoSub Servo_loop_fast
    If BtnNext = 0 Then
        DelayMS 25                  ;Small delay in case pushbutton was not pressed down hard enough.
        While BtnNext = 0 : Wend    ;Wait until pushbutton is released.
        Menu = 3 ;Next
        GoTo Loop
    EndIf
   
;    If BtnPrev = 0 Then
;        DelayMS 25                  ;Small delay in case pushbutton was not pressed down hard enough.
;        While BtnPrev = 0 : Wend    ;Wait until pushbutton is released.
;        Menu = 1 ;Next
;        GoTo Loop
;    EndIf
Wend

Menu3:
Cls
Print At 1, 1, "Servo Slow     "
While 1 = 1
    GoSub Servo_loop_slow
    If BtnNext = 0 Then
        DelayMS 25                  ;Small delay in case pushbutton was not pressed down hard enough.
        While BtnNext = 0 : Wend    ;Wait until pushbutton is released.
        Menu = 4 ;Next
        GoTo Loop
    EndIf
   
;    If BtnPrev = 0 Then
;        DelayMS 25                  ;Small delay in case pushbutton was not pressed down hard enough.
;        While BtnPrev = 0 : Wend    ;Wait until pushbutton is released.
;        Menu = 2 ;Next
;        GoTo Loop
;    EndIf
Wend

Menu4:
Cls
Print At 1, 1, "Variable speed"
While 1 = 1
    GoSub Servo_loop_variable_speed
    If BtnNext = 0 Then
        DelayMS 25                  ;Small delay in case pushbutton was not pressed down hard enough.
        While BtnNext = 0 : Wend    ;Wait until pushbutton is released.
        Menu = 0 ;Next
        GoTo Loop
    EndIf
   
;    If BtnPrev = 0 Then
;        DelayMS 25                  ;Small delay in case pushbutton was not pressed down hard enough.
;        While BtnPrev = 0 : Wend    ;Wait until pushbutton is released.
;        Menu = 3 ;Next
;        GoTo Loop
;    EndIf
Wend

Start:

;Init
Clear                         ;Erase RAM memory
DelayMS 500                   ;Stabilize LCD
Cls                           ;Erase LCD
Menu = 0                      ;Force Menu0

;Main
Loop:
BranchL Menu, [Menu0,Menu1,Menu2,Menu3,Menu4]  ;If Menu=0 go to Menu0 subroutine and don't come back unless told to.  If Menu=1 go to Menu1 subroutine and... etc.
GoTo Loop

End                           ;End of program
thaking



Joined: 22 Jan 2011
Posts: 25

View user's profile Send private message

PostPosted: Fri Mar 11, 2011 12:16 pm     Reply with quote

I started with:
Code:

#include <16F877A.h>
#device ADC=10
#fuses HS, NOWDT, NOPUT, NOLVP, NOPROTECT
#use delay(clock=10M)

#include "flex_lcd.c"

#define button PIN_A3   

//=============================
void main() {
set_tris_a(0xff);
set_tris_d(0x00);
setup_adc_ports(NO_ANALOGS);
delay_us(50);

lcd_init();  // Always call this first.

printf(lcd_putc, "\f Servo center\n");

while(1);
}
sseidman



Joined: 14 Mar 2005
Posts: 159

View user's profile Send private message

PostPosted: Fri Mar 11, 2011 12:56 pm     Reply with quote

Shouldn't you take a crack at it before asking for help??

"Translating" is a bit of a loser. Put the time in to figure out what's going on in the Basic program, and take a fresh start at implementing it in C. Break this down into logical steps that you can implement and test one at a time, and if you have problems with any of these steps, I'm sure you'll get all the help you need here to work it through.

Just taking a peek at your Basic code--

DelayMS 20 ;This will result in the pulse being transmitted 50x per second

This isn't a great way to go about something like this. You'd be better off learning how to time your program off the timer interrupts. It may be fine the way you're doing it, but it isn't great form.
thaking



Joined: 22 Jan 2011
Posts: 25

View user's profile Send private message

PostPosted: Fri Mar 11, 2011 1:13 pm     Reply with quote

Ok tnx for advice, can you please tell me how to make

menu's on LCD, like
Quote:
1.Center Servo
2.Manual mode
3.Servo Fast
4.Variable speed


while I'm pressing button (#define button PIN_A3 )

Thanks
temtronic



Joined: 01 Jul 2010
Posts: 9188
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Mar 11, 2011 1:29 pm     Reply with quote

Heck , I'm impressed the original programmer comented almost every line of code! Must be an old guy like me ,self taught in assembler then onto the high level stuff!

The only way you'll understand what's going on is to translate it yourself. This is easy as the comments really help. Since you're already fluent in that Basic, it might only take you a day or two to get it 'up and running'.
You have the benefit of the Web, Google and various forums unlike 30 years ago....

PS. Obviously you have a 4 line LCD....
PPS. Search this forum for a 'button' function, it's here....
thaking



Joined: 22 Jan 2011
Posts: 25

View user's profile Send private message

PostPosted: Fri Mar 11, 2011 2:11 pm     Reply with quote

temtronic wrote:
Heck , I'm impressed the original programmer comented almost every line of code! Must be an old guy like me ,self taught in assembler then onto the high level stuff!

The only way you'll understand what's going on is to translate it yourself. This is easy as the comments really help. Since you're already fluent in that Basic, it might only take you a day or two to get it 'up and running'.
You have the benefit of the Web, Google and various forums unlike 30 years ago....

PS. Obviously you have a 4 line LCD....
PPS. Search this forum for a 'button' function, it's here....


I am, but I didn't find what I am lookng, which is like menu's.

I know that it something in this way:

if
case 0

case 1

...

but I don't find exactly...

thanks
sseidman



Joined: 14 Mar 2005
Posts: 159

View user's profile Send private message

PostPosted: Fri Mar 11, 2011 5:52 pm     Reply with quote

There are probably 4 or 5 different constructs you could use to do this. The one that probably comes closest to a menu is

switch case

Just search for help on "switch" in the compiler.

Try not to think of things like "an LCD menu is controlling my program". You'll tie yourself up in knots.

You have program, that accepts inputs from things like buttons. Your program acts on button pushes, and also modifies the LCD.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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