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.

Problem with C and PIC16F628A

Status
Not open for further replies.

Andrei917

Newbie level 5
Joined
Sep 5, 2012
Messages
8
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,392
Hi all, first I need to say I'm not good with microcontrollers, and I am building my second tesla coil, that is why I need a tachometer for the rotary spark gap RPM.

I started to look everywhere for a tachometer project, but I'm a bit limited on "money". I was trying to use PIC, because I can use pickit programmer at work. Then I found a code and schematics using PIC16F628A and 4 digit 7 segment display on the internet. Since I have this pic at work, my boss let me use it to test, if it works, then I will buy the parts.

I build the wiring as a lot of PIC projects:

RA0-3 ports control the digits
RB0-6 ports the segments
RA4 signal from IR transistor
RB7 turns on a IR led
MCRL goes to +5v
4MHz Crystal

Still the thing don't work. It shows 0000 when motor is off. If i turn on the motor, it show 8888, sometimes 8808, or some random segments that don't look like a number nor a letter.

I used this code from a blog. I found somewhere at this forum that a "break" comes after the case 0,1,2... lines, still it don't work.


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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
unsigned short mask(unsigned short num) {
  switch (num) {
    case 0 : return 0x3F;
    case 1 : return 0x06;
    case 2 : return 0x5B;
    case 3 : return 0x4F;
    case 4 : return 0x66;
    case 5 : return 0x6D;
    case 6 : return 0x7D;
    case 7 : return 0x07;
    case 8 : return 0x7F;
    case 9 : return 0x6F;
  } //case end
}
sbit IR_Tx at RB7_bit;
sbit DD0_Set at RA0_bit;
sbit DD1_Set at RA1_bit;
sbit DD2_Set at RA2_bit;
sbit DD3_Set at RA3_bit;
unsigned short i, DD0, DD1, DD2, DD3;
unsigned int Sample1, Sample2, Sample3, RPM;
void main() {
 
TRISB = 0b00000000; // Set PORTB direction to be output
TRISA = 0b00110000; // Set PORTA direction to be output
PORTB = 0x00; // Turn OFF LEDs on PORTB
CMCON = 7 ; // Disable comparators
RPM = 0; // Initial Value of Counter
OPTION_REG = 0b00111000; // TOCS=1 for Counter mode, PSA=1 for 1:1
IR_Tx = 0; // Turn OFF IR
 
do {
 
 DD0 = RPM%10;
 DD0 = mask(DD0);
 DD1 = (RPM/10)%10;
 DD1 = mask(DD1);
 DD2 = (RPM/100)%10;
 DD2 = mask(DD2);
 DD3 = (RPM/1000);
 DD3 = mask(DD3);
 
for (i = 0; i<=100; i++) {
PORTB = DD0;
DD0_Set = 1;
DD1_Set = 0;
DD2_Set = 0;
DD3_Set = 0;
delay_ms(5);
PORTB = DD1;
DD0_Set = 0;
DD1_Set = 1;
DD2_Set = 0;
DD3_Set = 0;
delay_ms(5);
PORTB = DD2;
DD0_Set = 0;
DD1_Set = 0;
DD2_Set = 1;
DD3_Set = 0;
delay_ms(5);
PORTB = DD3;
DD0_Set = 0;
DD1_Set = 0;
DD2_Set = 0;
DD3_Set = 1;
delay_ms(5);
}
DD3_Set = 0;
 
// First Sample
TMR0=0;
IR_Tx = 1;
Delay_ms(1000); // Delay 1 Sec
IR_Tx = 0;
Sample1 = TMR0*60;
 
// Second Sample
TMR0=0;
IR_Tx = 1;
Delay_ms(1000); // Delay 1 Sec
IR_Tx = 0;
Sample2 = TMR0*60;
 
// Third Sample
TMR0=0;
IR_Tx = 1;
Delay_ms(1000); // Delay 1 Sec
IR_Tx = 0;
Sample3 = TMR0*60;
 
RPM = Sample1 + Sample2 + Sample3;
RPM = RPM/3;
 
} while(1); // endless loop
}

 
Last edited by a moderator:

Hi, I read that thread, but there is no store selling PIC18F14k50 here. Only if I buy from US. I would but mail here is terrible, it will take 4-6 months to get the product. I will use optic IR led and transistor. The hall sensor needs a magnet, and I'm afraid if it will work properly will the very high voltage around.

Atmega is impossible right now, I can't buy the programmer, things are expensive here. The other project with PIC 18f452, maybe I can find that microcontroller around here, I was trying to use 7 segments display, which I can read from very far*, but if this one with 18f452 works, I will use LCD... *I can't stay close to the coil.

I have been trying to understand how to use C with PIC, but I'm not good with technical english vocabulary, actually google translate is helping me on this post heh. Maybe I can find a book in portuguese.

Well I have to think a bit about what I'm going to do, I can spend around 25$ this month... I almost got a chinese tachometer, but they only use 1 "pulse" / rotation, that is not very precise in low RPMs. This part of the code I could edit, to count more pulses/rotation, but their tachometer is probably code protected. My problem is the part where the code "talks" with PIC, the ports, LCD.
 
Last edited:

The designs discussed in the thread can be easily ported to the PIC16F series, there is no need to purchase a PIC18F device.

BigDog
 

I didn't knew that was possible. I will study how to do that. Thank you.
 

As long as the PIC provides the required feature or peripheral module utilized by the design/code, you should be able to port the source to a particular device.

BigDog
 

The first code shown works (sort of) but the delays cause the display to turn off and back on again on about a 3 second cycle.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top