Can u help me to convert my C programm to hex code for PIC microcontroler..?????

Started by pradeep_K_B, June 15, 2012, 07:29:10 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

pradeep_K_B

hello...i need to program a PIC 16f690 for to drive my servo motor....i have the C programm with me,but i dont know how to convert it to hex...i already tried in different ways...but.....can u help me to take the hex code from my C programm...pls

ralsonjemmy

Hello,
that need to be 'compiled' to create a hex , do you have any info on which compiler was used to build ?

Regards
-RJ

pradeep_K_B

i have tried it with sourceboost,,but still i cant.....i dont have a standard PIC programmer with me like pickit2 or some thing,,,,i think thats the problem...but once i got the hex code i can programm my pic with my  pic programmer

Swapnil

Pradeep, send me the C file, I'll convert it to hex and send it back to you! :)

Dharmik


Swapnil


pradeep_K_B


Swapnil

Yes, like I said in reply #3, send me the C file... I'll convert it to hex and send it back to you!

pradeep_K_B


pradeep_K_B

// ***************************************************************************
//  File Name    : servo.c
//  Version      : 1.0
//  Description  : Servo Motor Controller
//                 Using TIMER0 for Generating Servo PWM
//  Target       : PICJazz 16F690 Board
//  Compiler     : HITECT PICC-Lite Version 9.60PL1
//  IDE          : Microchip MPLAB IDE v8.00
//  Programmer   : PICKit2
// ***************************************************************************
#include <pic.h>
/*   PIC Configuration Bit:
**   INTIO     - Using Internal RC No Clock
**   WDTDIS    - Wacthdog Timer Disable
**   PWRTEN    - Power Up Timer Enable
**   MCLREN    - Master Clear Enable
**   UNPROTECT - Code Un-Protect
**   UNPROTECT - Data EEPROM Read Un-Protect
**   BORDIS    - Borwn Out Detect Disable
**   IESODIS   - Internal External Switch Over Mode Disable
**   FCMDIS    - Monitor Clock Fail Safe Disable
*/
__CONFIG(INTIO & WDTDIS & PWRTEN & MCLREN & UNPROTECT \
  & UNPROTECT & BORDIS & IESODIS & FCMDIS);
// Using Internal Clock of 8 Mhz
#define FOSC 8000000L
// Servo definition and variables
#define MAX_VALUE 200
#define CCW_ROTATION MAX_VALUE - 20
#define CW_ROTATION MAX_VALUE - 10
#define STOP_ROTATION MAX_VALUE
#define THRESHOLD_VALUE 50
unsigned char pulse_max=0;
unsigned char pulse_top=0;
unsigned char top_value = 0;
static void interrupt isr(void)
{
  if(T0IF) {             // 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;
      RC2=0;                // Turn off RC2
    }

    /* top_value = MAX_VALUE - n, n=10: 10 x 0.1ms = 1.0ms, n=20: 20 x 0.1ms = 2.0ms */
    /* 2ms -> CCW Rotation, 1ms -> CW Rotation */
    if (pulse_top == top_value) {
      RC2=1;                // Turn On RC2
    }
    TMR0 = 156;             // Initial Value for 0.1ms Interrupt
    T0IF = 0;     // Clear TIMER0 interrupt flag
  }
}
void main(void)
{
  unsigned char ldr_left;
  unsigned char ldr_right;
  int ldr_diff;
  OSCCON=0x70;         // Select 8 Mhz internal clock
  /* Initial Port Used */
  TRISC = 0x03;        // Set RC0 and RC1 as input others as Output
  ANSEL = 0x30;        // Set PORT AN4 and AN5 as analog input
  ANSELH = 0x00;       // Set PORT AN8 to AN11 as Digital I/O
  PORTC = 0x00;        // Turn Off all PORTC
  /* Init Servo Pulse */
  pulse_max=0;
  pulse_top=0;
  top_value = MAX_VALUE; // top_value = MAX_VALUE: Servo Motor Stop
  /* Initial ADC */
  ADCON1=0b00110000;   // Select the FRC for 8 Mhz
  /* Init TIMER0: Period: Fosc/4 x Prescaler x TMR0
     0.0005 ms x 2 * 100 = 0.1 ms */
  OPTION = 0b00000000; // 1:2 Prescaller
  TMR0=156;            // Interupt every 0.1 ms
  T0IE = 1;        // Enable interrupt on TMR0 overflow
  GIE = 1;        // Global interrupt enable

  for(;;) {
    /* Read the ADC here */
    ADCON0=0b00010001;       // select left justify result. ADC port channel AN4
    GODONE=1;              // initiate conversion on the channel 4
    while(GODONE) continue;  // Wait for ldr_left conversion done
    ldr_left=ADRESH;         // Read 8 bits MSB, Ignore 2 bits LSB in ADRESL
    ADCON0=0b00010101;       // select left justify result. ADC port channel AN5
    GODONE=1;              // initiate conversion on the channel 5
    while(GODONE) continue;  // Wait for ldr_right conversion done
    ldr_right=ADRESH;        // Read 8 bits MSB, Ignore 2 bits LSB in ADRESL

    /* 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
      }
    }
  }
}
/* EOF: servo.c */

pradeep_K_B


Swapnil

Hmm...Okay can you please wait till afternoon, I'm a bit busy right now. I'll install PICC-Lite and send the hex to you. I also have MicroC pro, will check a servo program there too. Hang tight!

pradeep_K_B


Swapnil

Pradeep, why don't you try an Arduino? It's cheap, simple and hassle-free.
And it has a dedicated servo library! So you only need 3 lines of code!


#include <Servo.h>
Servo servo1;

-----main loop----

servo1.writeMicroseconds(1765);

--------------------

That's it!


pradeep_K_B

but i dont have a Adruino programmer..i have only the PIC programmer with me...thats y :(

Swapnil


pradeep_K_B


Swapnil


pradeep_K_B


pradeep_K_B


Swapnil


pradeep_K_B


pradeep_K_B

its the code

;**********************************************************************
;                                                                     *
;    Filename:     encoder.asm                                       *
;    Date:                                                            *
;    File Version:                                                    *
;                                                                     *
;**********************************************************************
;NOTES
;encoder v4.2
;
;**********************************************************************
;HISTORY
;
; 040-20050131 4/8-channel encoder for 16F630
; 041-20050213
; 042-20050217 modify transmission repetition algorithm
; 043-20050227 use IR medium with irmtxv4 library
; 044-20090531 adapt 042-20050227 to the pic16f628
;
;**********************************************************************
list p=16f628
__CONFIG   _CP_OFF & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT & _LVP_OFF & _MCLRE_OFF & _BODEN_OFF


; toggle or momentary mode, 8 channels
#define MODE_CH8

; ON/OFF latched mode, 4 channels
;#define MODE_CH4

#include <p16F628.inc>
#include <mtxv4.inc>

variables UDATA
; RAM registers
tcnt RES 1
rcnt RES 1
cod RES 1
prevcod RES 1
cod0 RES 1
rowstate RES 1

startup CODE 0
  goto main
nop
nop
nop
retfie

prog CODE

main ; program starts here
clrf PORTA
clrf PORTB
clrf TMR0

BANKSEL TRISA
movlw 0
movwf TRISA
movlw 0xf0
movwf TRISB

bcf OPTION_REG, PSA
clrwdt
clrf OPTION_REG
clrwdt

BANKSEL PORTA
movlw 7
movwf CMCON
movlw (1<<RBIE); RB4-7 int on change enable
movwf INTCON

call mtx_init
clrf mtx_buffer
clrf tcnt

loop0 clrf (mtx_buffer+1)
movlw 0xff
movwf prevcod ; no button was pressed in the previous scan
movlw 0xfc ;ca,cb=0,cj=1
movwf PORTB
movlw 0xf0
tris PORTB
movf PORTB, W
bcf INTCON, RBIF
sleep

loop clrf cod
movlw 0xfe
tris PORTB ; select colA (RB0)
clrf PORTB
#ifdef MODE_CH8
clrw
#endif
#ifdef MODE_CH4
movlw 0x20
#endif
call scan
movlw 0xfd
tris PORTB ; select colB (RB1)
#ifdef MODE_CH8
movlw 0x04
#endif
#ifdef MODE_CH4
movlw 0x30
#endif
call scan
movf cod, W
bz loop2 ; if no buton is pressed, skip

subwf prevcod, W ; if the same button is pressed, skip
bz loop2

movf cod, W
movwf prevcod ; a new button is pressed - rcnt=3
movwf (mtx_buffer+1)
movlw 3
movwf rcnt

movlw 0x40 ; new button - new transmission
addwf tcnt, F

loop2 movlw 0xf7
tris PORTB ; select ID (RB3)
call scanid
movf cod0, W
movwf (mtx_buffer)

loop3 movf (mtx_buffer+1), W
andlw 0x3f
iorwf tcnt, W
movwf (mtx_buffer+1)

call mtx_send

movf rcnt, W
bz loop_done
decfsz rcnt, F
goto loop

loop_done movf cod, W
btfsc STATUS, Z
goto loop0 ; no button was pressed, go sleep
; if the same button is being hold, repeat the transmission
goto loop

scan movwf cod0
;movlw 0xc0
scandelay ;addlw 1
;bnz scandelay
movlw 0xf0
andwf PORTB, W
movwf rowstate

incf cod0, F
btfss rowstate, 4
goto pressed

incf cod0, F
btfss rowstate, 5
goto pressed

incf cod0, F
btfss rowstate, 6
goto pressed

incf cod0, F
btfss rowstate, 7
goto pressed
retlw 0

pressed movf cod0, W
movwf cod
return

scanid clrf cod0
clrw
scandelay2 addlw 1
bnz scandelay2
movlw 0xf0
andwf PORTB, W
movwf rowstate

btfss rowstate, 7
bsf cod0, 3
btfss rowstate, 6
bsf cod0, 2
btfss rowstate, 5
bsf cod0, 1
btfss rowstate, 4
bsf cod0, 0
return

end