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.

Help on changing c code from picf12 to pic16f

Status
Not open for further replies.

Nojman

Newbie level 6
Joined
Jan 11, 2014
Messages
11
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
108
Hi!

I have build a clapper from this site https://embedded-lab.com/blog/?p=6439
He used a PIC12F683 but I hade a PIC16F628 home so I used it.
I wonder if someone could help med with the c code? And correct me if I have put the wires to the right pins.
I have tried to read the datasheets for both the PICs, on the pins and how to change the configuration bits by but I have a hard time to understand it fully.

I have wired the pins like this from the site.
Code:
/*
               ________  ________
              |        \/        |
T1 and R10K---|RA2   16F628   RA1|
              |RA3            RA0|
              |RA4-od    RA7/OSC1|
              |RA5/MCLR  RA6/OSC2|
       GND ---|Vss            Vdd|--- +5V
       	      |RB0/INT  (RB7)/PGD|
              |RB1/Rx   (RB6)/PGC|--- the LED with R330
              |RB2/Tx         RB5|
              |RB3/CCP  (RB4)/PGM|
              |__________________|
*/
(I have wires to the pins)



And here is the code with my changes.

Code:
//sbit Output_LED at GP5_bit; on the original code
#define Output_LED PORTD.F3;

//sbit led_led at PORTc.c2;
unsigned short i, TIME_UP;

 void interrupt(void){
 if(PIR1.TMR1IF){
   i ++;
   if(i == 3) TIME_UP = 1; // Time Up in 1.5 sec 
   PIR1.TMR1IF = 0;
  } 
}

void main() {
  TRISIO = 0b00000011 ;
  //ANSEL = 0x00; on the original code
  INTCON = 0b11000000 ; // Enable GIE and PEIE for Timer1 overflow interrpt
  PIE1 = 0b00000001 ; // Enable TMR1IE

  // Configure Comparator module
  // CIN- pin is configured as analog,
  // CIN+ pin is configured as I/O,
  // COUT pin is configured as I/O,
  // Comparator output available internally,
  // CVREF is non-inverting input 
  // CINV is set to 1

  COMCON = 0b00010100; 
  VRCON = 0b10100011; // Vref is set to VDD/8
  Output_LED = 0;
  do{ TMR1H = 0x00;
     TMR1L = 0x00;
     TIME_UP = 0;
     i = 0;
     T1CON = 0b00110000; // Configure Timer 1 
     if(CMCON0.COUT){ // First clap detected
       Delay_ms(100);
       T1CON.TMR1ON = 1; // Start Timer1
       while(!COMCON.COUT && !TIME_UP); // Wait until second clap is 
       T1CON.TMR1ON = 0; // detected or Timer1 overflows
       if(COMCON.COUT && !TIME_UP) Output_LED =  ~Output_LED; 
       Delay_ms(100); 
     } 
  }   while(1);
}




//- See more at: http://embedded-lab.com/blog/?p=6439#sthash.YHvZk7SY.dpuf


My error message..

Code:
Clean: Deleting intermediary and output files.
Clean: Done.
Executing: "C:\Program Files (x86)\PK2proj\Cc5x\CC5X.EXE" clapper.c -CC -fINHX8M -p16F628 -a -L -Q -V -FM
CC5X Version 3.3A, Copyright (c) B Knudsen Data, Norway 1992-2007
 --> FREE edition, 8-16 bit int, 24 bit float, 1k code, reduced optim.
clapper.c:

 void first_interrupt( void){
  if(PIR1.TMR2IF){
               ^------
Error[1] C:\Program Files (x86)\PK2proj\Work\clapper.c 31 : Syntax error
 (The expression must be corrected. The marker ^------ shows
 the approximate position where CC5X gives up. The documentation
 should be checked if the cause is not obvious)

Error options: -ew: no warning details  -ed: no error details  -eL: list details
BUILD FAILED: Wed Jan 15 01:36:50 2014



(I'm using MPLAB IDE 8.92, PICkit 3, B Knudsen Data CC5X)

I would be realy thankfull if someone could help me :)
 
Last edited:

Maybe I'm missing something but your full source has:

Code:
 void interrupt(void){
 if(PIR1.TMR1IF){

and the error has:

Code:
 void first_interrupt( void){
  if(PIR1.TMR2IF){

I don't see the function with the error in your code.
 
Last edited by a moderator:
  • Like
Reactions: Nojman

    Nojman

    Points: 2
    Helpful Answer Positive Rating
hello,

you don't have PORTD on 16F628

Code:
#define Output_LED PORTD.F3;

Code:
#define Output_LED PORTB.F6;

TRISA=0b11111111;
TRISB=0b10111111;
 
  • Like
Reactions: Nojman

    Nojman

    Points: 2
    Helpful Answer Positive Rating
Maybe I'm missing something but your full source has:

void interrupt(void){
if(PIR1.TMR1IF){

and the error has:

void first_interrupt( void){
if(PIR1.TMR2IF){

I don't see the function with the error in your code.

Thanks for your responsens.
I forgot to post my error message and between that I worked little on th code.
I think I have fixed som problems, got away some error messages but don't know if its gonna be correct.

I figured out half of the define yesterday but the F6 and the rest I didn't have. So thanks paulfjujo :)

Here is the code after I tryed to change it.

PHP:
//sbit Output_LED at GP5_bit;
#define Output_LED PORTB.F6;
//#pragma config MCLR_ON 
//#pragma config PWRT = ON //enabled
//#pragma config |= 0x3fb0


#include "int16Cxx.h" 
#pragma config |= 0x3F82 





//sbit led_led at PORTc.c2;
unsigned short i, TIME_UP;

 void first_interrupt( void){
  if(PIR1 == 1){  //.TMR2IF
 	i ++;
 	if(i == 3)TIME_UP = 1; // Time Up in 1.5 sec 
	PIR1 = 0;  //.TMR2IF
  } 
}

 void main() {
 TRISA = 0b11111111;  
TRISB = 0b10111111; //TRISIO
  //ANSEL = 0x00;
  INTCON = 0b11000000 ; // Enable GIE and PEIE for Timer1 overflow interrpt
  PIE1 = 0b00000001 ; // Enable TMR1IE

  // Configure Comparator module
  // CIN- pin is configured as analog,
  // CIN+ pin is configured as I/O,
  // COUT pin is configured as I/O,
  // Comparator output available internally,
  // CVREF is non-inverting input 
  // CINV is set to 1
  CMCON = 0b00010100; //MÅSTE ÄNDRAS
  VRCON = 0b10100011; // Vref is set to VDD/8
  Output_LED = 0;
  do{ 
	TMR1H = 0x00;
	TMR1L = 0x00;
	TIME_UP = 0;
	i = 0;
	T1CON = 0b00110000; // Configure Timer 1 
	if(CMCON.COUT){ // First clap detected
	  Delay_ms(100);
  	  T1CON.TMR1ON = 1; // Start Timer1
  	  while(!COMCON.COUT && !TIME_UP); // Wait until second clap is //CMCON0 MÅSTE ÄNDRAS
  	  T1CON.TMR1ON = 0; // detected or Timer1 overflows
  	  if(COMCON.COUT && !TIME_UP) Output_LED =  ~Output_LED; //CMCON0 MÅSTE ÄNDRAS
	  Delay_ms(100); 
	} 
  } while(1);
 }


and the error meassage now.


PHP:
Clean: Deleting intermediary and output files.
Clean: Done.
Executing: "C:\Program Files (x86)\PK2proj\Cc5x\CC5X.EXE" clapper.c -CC -fINHX8M -p16F628 -a -L -Q -V -FM
CC5X Version 3.3A, Copyright (c) B Knudsen Data, Norway 1992-2007
 --> FREE edition, 8-16 bit int, 24 bit float, 1k code, reduced optim.
clapper.c:

  VRCON = 0b10100011; // Vref is set to VDD/8
  Output_LED = 0;
           ^------
Error[1] C:\Program Files (x86)\PK2proj\Work\clapper.c 59 : Syntax error
 (The expression must be corrected. The marker ^------ shows
 the approximate position where CC5X gives up. The documentation
 should be checked if the cause is not obvious)

//sbit Output_LED at GP5_bit;
#define Output_LED PORTB.F6; //måste ha RB6
                 ^------
Error[2] C:\Program Files (x86)\PK2proj\Work\clapper.c 20 : Previous error is at macro 'Output_LED'

Error options: -ew: no warning details  -ed: no error details  -eL: list details
BUILD FAILED: Wed Jan 15 19:00:38 2014

(is it better to show the code in CODE or PHP?)

Thank you very much
 

Okay, then I understand better. I changed it it to
#define Output_LED PORTB.RB6
but it didn't work untill I changed it to
#define Output_LED RB6

Now I get a error message on the
Delay10(100); (It was
if(CMCON0.COUT){ Delay_ms(100);
before I changed it)
and on
T1CON.TMR1ON = 1;
I checked the datacheet and it has the same, T1CON where the last bit is TMR1ON. I'm I reading it correctly?

The error message..
PHP:
Clean: Deleting intermediary and output files.
Clean: Done.
Executing: "C:\Program Files (x86)\PK2proj\Cc5x\CC5X.EXE" clapper.c -CC -fINHX8M -p16F628 -a -L -Q -V -FM
CC5X Version 3.3A, Copyright (c) B Knudsen Data, Norway 1992-2007
 --> FREE edition, 8-16 bit int, 24 bit float, 1k code, reduced optim.
clapper.c:

	if(CMCON){ // First clap detected
	  Delay10(100);
	         ^------
Error[1] C:\Program Files (x86)\PK2proj\Work\clapper.c 69 : Symbol 'Delay10' is undefined
 (The definition of the symbol in not known. This can be due to
 wrong spelling or letter case mismatch. Otherwise a new symbol
 definition is required: variable, function, macro, etc.)

	  Delay10(100);
  	  T1CON.TMR1ON = 1; // Start Timer1
  	             ^------
Error[2] C:\Program Files (x86)\PK2proj\Work\clapper.c 70 : Syntax error
 (The expression must be corrected. The marker ^------ shows
 the approximate position where CC5X gives up. The documentation
 should be checked if the cause is not obvious)

Error options: -ew: no warning details  -ed: no error details  -eL: list details
BUILD FAILED: Sat Jan 18 21:34:13 2014

Thanks for the help paulfjujo :)
 

See if this compiles. Not tested.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
 Project: A very basic clap switch
 Description : Clap switch using built-in comparator module
               of PIC16F628
 MCU: PIC16F628
 Oscillator: Internal 4.0000 MHz, MCLR Disabled, PWRT ON enabled
 Written by: Jayanth Devarayanadurga
 Date:       Jan 19, 2014
*/
//#include <16F628.H>
 
#pragma config WDTE=off, FOSC=XT
 
#define Output_LED PORTB.6
unsigned char i, TIME_UP;
 
#pragma origin 4
interrupt int_server(void)
{
    if(TMR1IF) {
        i ++;
        if(i == 3) TIME_UP = 1;  // Time Up in 1.5 sec
        TMR1IF = 0;
    }
    
}
 
void delay(char millisec)
    /* delays a multiple of 1 millisecond at 4 MHz */
    {
    OPTION = 2; /* prescaler divide by 8 */
    do {
        TMR0 = 0;
        clrwdt(); /* only if necessary */
        while (TMR0 < 125) /* 125 * 8 = 1000 */
                ;
    } while (-- millisec > 0);
}
 
 
void main() {
  TRISA = 0xFF;   
  TRISB = 0b10111111;
  INTCON = 0xC0 ; // Enable GIE and PEIE for Timer1 overflow interrpt 
  PIE1 = 0x01 ; // Enable TMR1IE 
 
  // Configure Comparator module
  // CIN- pin is configured as analog, 
  // CIN+ pin is configured as I/O,
  // COUT pin is configured as I/O,
  // Comparator output available internally,
  // CVREF is non-inverting input
  
  CMCON = 0b00010100;
  VRCON  = 0b10100011;  // Vref is set to VDD/8
  Output_LED = 0;
 
  do{
     TMR1H = 0x00;
     TMR1L = 0x00;
     TIME_UP = 0;
     i = 0;
     T1CON = 0b00110000;   // Configure Timer 1
     if(C1OUT){      // First clap detected
       delay(10);
       TMR1ON = 1;   // Start Timer1
       while((!C1OUT) && (!TIME_UP)); // Wait until second clap is
       TMR1ON = 0;                      // detected or Timer1 overflows
       if((C1OUT) && (!TIME_UP))Output_LED = ~Output_LED;
       delay(10);
     }
  }  while(1);
}

 
  • Like
Reactions: Nojman

    Nojman

    Points: 2
    Helpful Answer Positive Rating
See if this compiles. Not tested.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
 Project: A very basic clap switch
 Description : Clap switch using built-in comparator module
               of PIC16F628
 MCU: PIC16F628
 Oscillator: Internal 4.0000 MHz, MCLR Disabled, PWRT ON enabled
 Written by: Jayanth Devarayanadurga
 Date:       Jan 19, 2014
*/
//#include <16F628.H>
 
#pragma config WDTE=off, FOSC=XT
 
#define Output_LED PORTB.6
unsigned char i, TIME_UP;
 
#pragma origin 4
interrupt int_server(void)
{
    if(TMR1IF) {
        i ++;
        if(i == 3) TIME_UP = 1;  // Time Up in 1.5 sec
        TMR1IF = 0;
    }
    
}
 
void delay(char millisec)
    /* delays a multiple of 1 millisecond at 4 MHz */
    {
    OPTION = 2; /* prescaler divide by 8 */
    do {
        TMR0 = 0;
        clrwdt(); /* only if necessary */
        while (TMR0 < 125) /* 125 * 8 = 1000 */
                ;
    } while (-- millisec > 0);
}
 
 
void main() {
  TRISA = 0xFF;   
  TRISB = 0b10111111;
  INTCON = 0xC0 ; // Enable GIE and PEIE for Timer1 overflow interrpt 
  PIE1 = 0x01 ; // Enable TMR1IE 
 
  // Configure Comparator module
  // CIN- pin is configured as analog, 
  // CIN+ pin is configured as I/O,
  // COUT pin is configured as I/O,
  // Comparator output available internally,
  // CVREF is non-inverting input
  
  CMCON = 0b00010100;
  VRCON  = 0b10100011;  // Vref is set to VDD/8
  Output_LED = 0;
 
  do{
     TMR1H = 0x00;
     TMR1L = 0x00;
     TIME_UP = 0;
     i = 0;
     T1CON = 0b00110000;   // Configure Timer 1
     if(C1OUT){      // First clap detected
       delay(10);
       TMR1ON = 1;   // Start Timer1
       while((!C1OUT) && (!TIME_UP)); // Wait until second clap is
       TMR1ON = 0;                      // detected or Timer1 overflows
       if((C1OUT) && (!TIME_UP))Output_LED = ~Output_LED;
       delay(10);
     }
  }  while(1);
}



Thank you very much jayanth.devarayanadurga

But I get this error message know. Tryed to fix it but I could not.. Any sugestions?

PHP:
Clean: Deleting intermediary and output files.
Clean: Done.
Executing: "C:\Program Files (x86)\PK2proj\Cc5x\CC5X.EXE" clapp_t2.c -CC -fINHX8M -p16F628 -a -L -Q -V -FM
CC5X Version 3.3A, Copyright (c) B Knudsen Data, Norway 1992-2007
 --> FREE edition, 8-16 bit int, 24 bit float, 1k code, reduced optim.
clapp_t2.c:
 Chip = 16F628
 RAM : -------- -------- -------- -------- =..***** ******** ******** ********
  40h: ******** ******** ******** ******** ******** ******** ******** ********
  80h: -------- -------- -------- -------- ******** ******** ******** ********
  C0h: ******** ******** ******** ******** ******** ******** -------- --------
 100h: -------- -------- -------- -------- ******** ******** ******** ********
 140h: ******** ********
RAM usage: 3 bytes (1 local), 221 bytes free

       TMR1ON = 0;                      // detected or Timer1 overflows
       if((C1OUT) && (!TIME_UP))Output_LED = ~Output_LED;
                                                        ^------
Error[1] C:\Program Files (x86)\PK2proj\Work\clapp_t2.c 69 : Unable to generate code
 (The C syntax is correct. However, CC5X is unable to generate code.
 The workaround is often to split the code into simpler statements,
 using an extra variable to store temporary results. Sometimes it is
 enough to change the sequence of operations)

Error options: -ew: no warning details  -ed: no error details  -eL: list details
BUILD FAILED: Sun Jan 19 14:03:39 2014
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top