PDA

View Full Version : Counting led blinks..



tacbanon
- 23rd September 2011, 16:37
Hi everyone, I have a coin acceptor that gives out pulse. Coins used are P1, P5 and P10. When I drop P1 it gives 1 blink to my led status, P5 it gives 5 blinks and P10 gives 10 blinks. I want to display the corresponding blinks on the LCD. I tried the following code and connected the .

DEFINE LCD_DREG PORTBDEFINE LCD_DBIT 0
DEFINE LCD_EREG PORTB
DEFINE LCD_EBIT 5
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBIT 4
DEFINE LCD_BITS 4
DEFINE LCD_LINES 2
DEFINE LCD_COMMANDUS 2000
DEFINE LCD_DATAUS 50
Define OSC 20
ADCON1 = 7 'Need this to turn off ADC

TRISB = 000011 'Pins A are in input mode
PortB = 0
W1 var byte
Clear


pause 100
Lcdout $fe, 1 ' Clear screen
Lcdout "Blink Counter" ' Display "Test Counter"
Init:
COUNT PORTB.1, 60, W1
Lcdout $fe, 1 ' Clear screen
Lcdout "Digital Designs"
Lcdout $fe, $c0, "Read: " , #W1
pause 1000
GOTO Init 'Do it always




But it displays erratic values. Can anyone guide me on this?
Diagram
5998

regards,
tacbanon

HenrikOlsson
- 23rd September 2011, 18:18
Hi,
How fast does the LED blink? You're only counting for 60ms so you need to insert the coin at exactly the right time - every time - for it be consistent.
I'd probably do this by setting up one of the PICs timers as a counter and wire the pulse signal to its input.

EDIT: Or something like:

Init:
While PortB.1 = 0 : WEND
Count PortB.1, 60, W1
Count = Count + 1
'LCDOUT......
'.....
Goto Init


/Henrik.

mister_e
- 24th September 2011, 10:19
Timer/Counter... motion seconded

tacbanon
- 25th September 2011, 14:29
@Henrik
Hi sorry for the delay, I can not tell exactly how fast it is. Can you give me a very simple example or a link that will help me out, I'm new with timers/counter. I was thinking it will be easy if I can count it like it is a button pressed..then just increment a variable counter.

regards,
tacbanon

HenrikOlsson
- 25th September 2011, 14:50
Hi,
You can count it like it's a button but the way you have it now means that the pulses have to come in exactly during the 60ms you're actually counting. If they start to come before you start to count you'll miss some of them or if the pusles starts 30ms after you've started to count you might stop counting before all pulses have arrived.

Just look at the datasheet for the PIC you're using, TMR0 is probably the easiest to use. Read the section, look at the T0CON register and you'll see how to configure it as a counter. The pulses then goes to T0CKI-pin and you can access the count by reading the TMR0 register.

Give it a try, if it doesn't work post the code (and what PIC you're using) and we'll take a look at it.

/Henrik.

Jumper
- 26th September 2011, 20:56
Interupt on change or even better a real INT pin. Then you can count all pulses without too much work. A good start is as always DT's instant interupts that makes it fast, fun and flexible.

mister_e
- 27th September 2011, 01:41
Why any real INT should be less work than any dedicated counter? No advanatage at all.

Q: Sir We need to know when something chagend, a button press
A: Load the counter to 255, on the next press it will generate an overflow interrupt

Jumper
- 28th September 2011, 22:27
Why? Since he already used PORTB.1 in his code an interrupt solution requires no HW change.

And it was just a example to show an other possibility....

tacbanon
- 30th September 2011, 06:00
Hi, Sorry for the delay. I got a working code that incorporate TMR0.

CLEAR DEFINE OSC 48
DEFINE LCD_DREG PORTB
DEFINE LCD_DBIT 0
DEFINE LCD_EREG PORTB
DEFINE LCD_EBIT 5
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBIT 4
DEFINE LCD_BITS 4
DEFINE LCD_LINES 2
DEFINE LCD_COMMANDUS 2000
DEFINE LCD_DATAUS 50
asm
;_PLLDIV_2_1L EQU H'F9' ; Divide by 2 (8 MHz oscillator input)

;__CONFIG _CONFIG1L, _PLLDIV_2_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L ' 1
__CONFIG _CONFIG1L, _PLLDIV_5_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
__CONFIG _CONFIG1H, _FOSC_HSPLL_HS_1H & _FCMEN_OFF_1H & _IESO_OFF_1H
;
;__CONFIG _CONFIG1L, _PLLDIV_1_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
; ; ; USB clock source comes from the 96 MHz PLL divided by 2
; ; [OSC1/OSC2 Src: /1][96 MHz PLL Src: /2]
; No prescale (4 MHz oscillator input drives PLL directly)




;__CONFIG _CONFIG1H, _FOSC_XTPLL_XT_1H & _FCMEN_OFF_1H & _IESO_OFF_1H '2
; ; ; Oscillator Switchover mode disabled
; ; Fail-Safe Clock Monitor disabled
; XT oscillator, PLL enabled, XT used by USB
__CONFIG _CONFIG2L, _PWRT_ON_2L & _BOR_ON_2L & _BORV_2_2L & _VREGEN_ON_2L
__CONFIG _CONFIG2H, _WDT_OFF_2H
__CONFIG _CONFIG3H, _MCLRE_ON_3H & _LPT1OSC_OFF_3H & _PBADEN_OFF_3H & _CCP2MX_ON_3H
__CONFIG _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _ICPRT_OFF_4L & _XINST_OFF_4L & _DEBUG_OFF_4L
endasm




INTCON = 100000 ' Enable global and TMR0 interrupts
T0CON = 000000 ' TMR0, CLK internal, prescaler 1:2, T0ON



ALPHA VAR WORD ;this variable counts in the PauseUS loop
BETA VAR BYTE ;this variable counts interrupt ticks
TRISD = 110100 ;sets the 3 output pins in the D port
PORTD = 000000 ;sets all pins low in the D port

ON INTERRUPT GOTO INTERUPTROUTINE ;This line needs to be early in the program, before
;the routine is called in any case.

MAINLOOP: ;Main loop blinks D0 and D1 alternately
IF PORTD.1 = 0 THEN ;]
PORTD.1 = 1 ;]
PORTD.0 = 0 ;] This part of the program blinks two LEDs in
ELSE ;] the foreground
PORTD.1 = 0 ;]
PORTD.0 = 1 ;]
ENDIF ;]

FOR ALPHA = 1 TO 300 ;The long pause is eliminated with this loop
PAUSEUS 100 ;PAUSE command with short latency
NEXT ALPHA ;
GOTO MAINLOOP ;end of loop

DISABLE ;DISABLE and ENABLE must bracket the interrupt routine
INTERUPTROUTINE: ;this information is used by the compiler only.
BETA = BETA + 1 ;
Lcdout $fe, 128, "Digital Output" ' Display
Lcdout $fe, $c0, "Counter: ", #BETA
IF BETA < 61 THEN ENDINTERRUPT ;one second has not yet passed
BETA = 0 ;
IF PORTD.3 = 1 THEN ;Interrupt loop turns D3 on and off every
PORTD.3 = 0 ;61 times through the interrupt routine.
ELSE ;That is about one second per full cycle
PORTD.3 = 1 ;
ENDIF ;
ENDINTERRUPT: ;
INTCON.2 = 0 ;clears the interrupt flag.
RESUME ;resume the main program
ENABLE ;DISABLE and ENABLE must bracket the int routine
END

I used "000 = 1:2 Prescale value( I think this is the fastest)" is this the pin that should be reading the pulse input?
I'm not really sure how to implement interrupt to the program yet, but I understand that its very important to learn this.

regards
tacbanon

HenrikOlsson
- 30th September 2011, 06:31
Hi,
Not sure exactly what you're trying to do with that specific piece of code.... If you're trying to setup a timer interrupt you're on the right track but if you're trying to use TMR0 as a counter to count the pulses it's not what you want.

When TMR0 is in timer-mode, like you have it, it counts internal instruction cycles "thru" the prescaler. If you clock the PIC at 4Mhz, one instruction cycle is 1us so the timer ticks along at 1Mhz/prescaler ratio. When it rolls over from 255 to 0 it sets the interrupt-flag.

When TMR0 is in counter-mode it counts transitions on the T0CKI-pin instead of internal instruction cycles. Again it sets the interrupt flag when it rolls over from 255 to 0 but it may not be of interest here.

Also, it looks like you're trying to set T0CON, INTCON, TRISD and PORTD "in binary" but there's no %-sign in front of the number. Also, try to always include all 8 bits. Like INTCON = %00100000 not INTCON=100000

/Henrik.

mister_e
- 30th September 2011, 23:47
FYI, the missing % is a new Code Box forum feature...

tacbanon
- 1st October 2011, 05:58
Hi,
I searched in the forum and found a peice of code by Bruce...


DEFINE LCD_DREG PORTBDEFINE LCD_DBIT 0
DEFINE LCD_EREG PORTB
DEFINE LCD_EBIT 5
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBIT 4
DEFINE LCD_BITS 4
DEFINE LCD_LINES 2
DEFINE LCD_COMMANDUS 2000
DEFINE LCD_DATAUS 50
PAUSE 100


TRISA.4 = 1 ' RA4/T0CKI = input to TMR0 counter
TRISB.0 = 0 ' RB0 = output for LED
CMCON = 7 ' All digital


i var byte
i=0
' Assign prescaler to WDT for 1:1 prescale on TMR0
' TMR0 clock will be from your external input on RA4/T0CKI
' Increment on high-to-low transitions
OPTION_REG = 111000

' If you prefer, then increment on low-to-high transitions
' OPTION_REG = 101000
Lcdout $fe, 128, "Counter: ", #i
pause 100
Main:
TMR0 = 0 ' Clear TMR0 count before start

Loop1:
WHILE TMR0 = 0 ' Wait for high-to-low transition
WEND ' on RA4/T0CKI
PORTB.0 = 1 ' LED on to indicate transition seen
i=i+1 ' Increment
Lcdout $fe, 128, "Counter: ", #i
PAUSE 200
PORTB.0 = 0 ' LED off
GOTO Main ' Start over
END

Everytime I press RA.4 it toggle RB0, I also put a variable that will display the value to the LCD, from the code I observed (please correct me if I wrong).
a. Timer0 is selected
b. The signal entry is RA.4
c. It increments on low-to-high transitions
d. Pre scale value is 1:2 (000 of Bits 2,1)
d. The configuration setting used OPTION_REG = 111000

So if connect the multi coin acceptor to the RA.4 this should count the number of pulse?

thanks in advance,
tacbanon

HenrikOlsson
- 1st October 2011, 07:10
Hi,
Yes, that would work. I'd probably add a little pause directly after the WHILE-WEND loop to allow the coin acceptor to push out all the pulses before the code reads the timer.

As for the prescaler, yes, the bits indicate a a 1:2 ratio but since bit 3 is set the prescaler isn't assigned to TMR0 but to the WDT so you get a 1:1 ratio which is what you want in this case.

Steve,
Thanks for the heads-up on the % character, great feature... Must try/verify:

This is a test, percent char %00110011
This is a test, no percent char 00110011
And here's how my post looks in preview:

6018
Looks fine in the preview. (Appologies to tachbanon for trying this here).
EDIT: And in the final post, I don't see a problem... Is it when there's a = infront?

OPTION_REG = %00110011
Again, looks fine in the preview.
EDIT: And in the final post.

/Henrik.

tacbanon
- 3rd October 2011, 06:55
Hi, now I'm trying to incorporate the coin acceptor, but I observered that when I connect PULSE line to RA4..it continues to count, even if the device is turned off or if I touched the wire. Please see the attachment, I'm using easypic6. What do you think causing it?


regards,
tacbanon

HenrikOlsson
- 3rd October 2011, 07:23
Hi,
Try adding a pullup or pulldown resistor on the input.

tacbanon
- 3rd October 2011, 07:51
I tried both using the built in switch to enabled Pull up/down.. no changes.:confused:
Please see attach image, I'm using the blue wire.
thanks,
tacbanon

tacbanon
- 3rd October 2011, 13:53
Hi, I finally got it right, what was missing was the GND.:o Thanks Henrik for the help, my next target is to run the codes on PIC18F4550..hope it wont be a problem.

regards,
tacbanon

tacbanon
- 5th October 2011, 09:21
Hi, I'm still having trouble converting my codes From PIC16F877A to PIC18F4550
I'm confused translating

' Assign prescaler to WDT for 1:1 prescale on TMR0' TMR0 clock will be from your external input on RA4/T0CKI
' Increment on high-to-low transitions
OPTION_REG = 111000 (Pic16F877A) to
T0CON = 10110111 (PIC18F4550) 'T0CON = TMR0ON T08BIT T0CS T0SE PSA T0PS2 T0PS1 T0PS0


Here is the whole code


'************************************************* **********************
DEFINE LCD_DREG PORTB
DEFINE LCD_DBIT 0
DEFINE LCD_EREG PORTB
DEFINE LCD_EBIT 5
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBIT 4
DEFINE LCD_BITS 4
DEFINE LCD_LINES 2
DEFINE LCD_COMMANDUS 2000
DEFINE LCD_DATAUS 50
PAUSE 100
asm
;_PLLDIV_2_1L EQU H'F9' ; Divide by 2 (8 MHz oscillator input)

;__CONFIG _CONFIG1L, _PLLDIV_2_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
;__CONFIG _CONFIG1L, _PLLDIV_1_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
; ; ; USB clock source comes from the 96 MHz PLL divided by 2
; ; [OSC1/OSC2 Src: /1][96 MHz PLL Src: /2]
; No prescale (4 MHz oscillator input drives PLL directly)
__CONFIG _CONFIG1L, _PLLDIV_5_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L


__CONFIG _CONFIG1H, _FOSC_XTPLL_XT_1H & _FCMEN_OFF_1H & _IESO_OFF_1H
; ; ; Oscillator Switchover mode disabled
; ; Fail-Safe Clock Monitor disabled
; XT oscillator, PLL enabled, XT used by USB
__CONFIG _CONFIG2L, _PWRT_ON_2L & _BOR_ON_2L & _BORV_2_2L & _VREGEN_ON_2L
__CONFIG _CONFIG2H, _WDT_OFF_2H
__CONFIG _CONFIG3H, _MCLRE_ON_3H & _LPT1OSC_OFF_3H & _PBADEN_OFF_3H & _CCP2MX_ON_3H
__CONFIG _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _ICPRT_OFF_4L & _XINST_OFF_4L & _DEBUG_OFF_4L
endasm
DEFINE OSC 48




TRISA.4 = 1 ' RA4/T0CKI = input to TMR0 counter
TRISA.0 = 0 ' RA0 = output for LED
CMCON = 7 ' All digital


i var byte
i=0
' Assign prescaler to WDT for 1:1 prescale on TMR0
' TMR0 clock will be from your external input on RA4/T0CKI
' Increment on high-to-low transitions

T0CON = 10110111 'T0CON = TMR0ON T08BIT T0CS T0SE PSA T0PS2 T0PS1 T0PS0

Lcdout $fe, 128, "Counter: ", #i
pause 100
Main:
TMR0 = 0 ' Clear TMR0 count before start

Loop1:
WHILE TMR0 = 0 ' Wait for high-to-low transition
WEND ' on RA4/T0CKI
pause 100
PORTB.0 = 1 ' LED on to indicate transition seen
i=i+1 ' Clear screen
Lcdout $fe, 128, "Counter: ", #i
PAUSE 100
PORTB.0 = 0 ' LED off
GOTO Main ' Start over


END



When compiled Error message..."Error[113] c:\pbp\pbppic18.lib 589 : Symbol not previously defined (TMR0)"
What do I'm missing in setting the bits for T0CON?


thanks in advance,
tacbanon

HenrikOlsson
- 5th October 2011, 09:57
Hi,
Have you looked at the datasheet for the 18F4550 and compared it against the 16F877A? What's the difference regarding TMR0?

First, T0CON = %10110111
Bit7=1: TMR0 ON
Bit6=0: TMR0 is a 16bit timer/counter
Bit5=1: Transition on T0CKI Pin
Bit4=1: Increment on falling edge
Bit3=0: Prescaler assigned to TMR0
Bit2-0 = 1: Prescaler ration 1:256

Is that what you want? Why do want a prescaler ratio of 256? And do you need it to be a 16bit mode?
Try T0CON = %11111000 instead, or change bit4 if you want rising edge.

If you do look in the datasheet(s) you'll see that on the 16F TMR0 is 8bits wide and there is one register for it (TMR0) but on the the 18F it can be either 8bit or 16bit. Therefor they have changed the register name from TMR0 to TMR0L (low) and TMR0H (high). You need to make the corresponding changes in your program.

/Henrik.

tacbanon
- 5th October 2011, 16:05
Hi, Little by little I'm learning to like the datasheet :o, I managed to compile the program, but there is no display on the Lcd, and also I noticed that RB0 is always high.

DEFINE LCD_DREG PORTBDEFINE LCD_DBIT 0
DEFINE LCD_EREG PORTB
DEFINE LCD_EBIT 5
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBIT 4
DEFINE LCD_BITS 4
DEFINE LCD_LINES 2
DEFINE LCD_COMMANDUS 2000
DEFINE LCD_DATAUS 50
PAUSE 100
'OSC is 20Mhz
asm
__CONFIG _CONFIG1L, _PLLDIV_5_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L


__CONFIG _CONFIG1H, _FOSC_XTPLL_XT_1H & _FCMEN_OFF_1H & _IESO_OFF_1H
; ; ; Oscillator Switchover mode disabled
; ; Fail-Safe Clock Monitor disabled
; XT oscillator, PLL enabled, XT used by USB
__CONFIG _CONFIG2L, _PWRT_ON_2L & _BOR_ON_2L & _BORV_2_2L & _VREGEN_ON_2L
__CONFIG _CONFIG2H, _WDT_OFF_2H
__CONFIG _CONFIG3H, _MCLRE_ON_3H & _LPT1OSC_OFF_3H & _PBADEN_OFF_3H & _CCP2MX_ON_3H
__CONFIG _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _ICPRT_OFF_4L & _XINST_OFF_4L & _DEBUG_OFF_4L
endasm
DEFINE OSC 48



TRISA.4 = 1 ' RA4/T0CKI = input to TMR0 counter
TRISA.0 = 0 ' RA0 = output for LED
TRISB.0 = 0 ' RB0 = output for LED
CMCON = 7 ' All digital


i var byte
i=0
' Assign prescaler to WDT for 1:1 prescale on TMR0
' TMR0 clock will be from your external input on RA4/T0CKI
' Increment on high-to-low transitions



' If you prefer, then increment on low-to-high transitions
T0CON = 11111000 'T0CON = TMR0ON T08BIT T0CS T0SE PSA T0PS2 T0PS1 T0PS0
'
Lcdout $fe, 128, "Counter: ", #i
pause 100
Main:
TMR0L = 0 ' Clear TMR0 count before start

Loop1:
WHILE TMR0L = 0 ' Wait for high-to-low transition
WEND ' on RA4/T0CKI
pause 100
PORTB.0 = 1 ' LED on to indicate transition seen
i=i+1 ' Clear screen
Lcdout $fe, 128, "Counter: ", #i
PAUSE 100
PORTB.0 = 0 ' LED off
GOTO Main ' Start over


END



What did i miss?

regards,
tacbanon

HenrikOlsson
- 5th October 2011, 16:30
Hi,
Not sure but the usual thing to check:
Does the pin you're trying to use have other functions mutliplexed to it, like ADC, Comparator, USB etc and does any of these peripherals need to be turned off for the pin to work in the mode you want. I see you've got the comparator covered but how about the ADC?

What do you mean by there is no display? Do you mean it doesn't the display the result or do you mean it doesn't display anything?

Again, check each pin against the datasheet, does have anything that may need to be turned off?

/Henrik.

tacbanon
- 5th October 2011, 22:51
hi,

What do you mean by there is no display? Do you mean it doesn't the display the result or do you mean it doesn't display anything?
No display on the Lcd

I see you've got the comparator covered but how about the ADC?

I dont think I'm gonna need ADC so I will need this...
ADCON0 = 000000 ' A/D converter off
ADCON1 = 15 ' make it digital
ucfg = 010100 ' I'm planning later on to use the usb

I'm not really sure if I'm getting it right, I'm struggling with the datasheet. The above added code has no effect..


tacbanon

tacbanon
- 6th October 2011, 01:37
Hi,
I got it, I have to remove ADCON0, just use ADCON1 = 15.
Thanks for your precious time, and "Thank GOD for PBP forum" :)

Next stop is to make this chip as usb cdc to pass the data to pc...

regards,
tacbanon

tacbanon
- 6th October 2011, 02:56
Hi, now I'm trying to create a usb cdc together with the reading pulse code.


asm ;__CONFIG _CONFIG1L, _PLLDIV_5_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
__CONFIG _CONFIG1L, _PLLDIV_2_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
__CONFIG _CONFIG1H, _FOSC_XTPLL_XT_1H & _FCMEN_OFF_1H & _IESO_OFF_1H
; ; ; Oscillator Switchover mode disabled
; ; Fail-Safe Clock Monitor disabled
; XT oscillator, PLL enabled, XT used by USB
__CONFIG _CONFIG2L, _PWRT_ON_2L & _BOR_ON_2L & _BORV_2_2L & _VREGEN_ON_2L
__CONFIG _CONFIG2H, _WDT_OFF_2H
__CONFIG _CONFIG3H, _MCLRE_ON_3H & _LPT1OSC_OFF_3H & _PBADEN_OFF_3H & _CCP2MX_ON_3H
__CONFIG _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _ICPRT_OFF_4L & _XINST_OFF_4L & _DEBUG_OFF_4L
endasm
DEFINE OSC 48
DEFINE LCD_DREG PORTB
DEFINE LCD_DBIT 0
DEFINE LCD_EREG PORTB
DEFINE LCD_EBIT 5
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBIT 4
DEFINE LCD_BITS 4
DEFINE LCD_LINES 2
DEFINE LCD_COMMANDUS 2000
DEFINE LCD_DATAUS 50
PAUSE 100


Include "cdc_desc.bas" ' Include the HID descriptors
buffer Var Byte[16]
Cnt VAR BYTE
Cnt = 16
TRISA.4 = 1 ' RA4/T0CKI = input to TMR0 counter
TRISA.0 = 0 ' RA0 = output for LED
CMCON = 7 ' All digital
ADCON1 = 001111 ' A/D converter off


' increment on low-to-high transitions
T0CON = 111000
USBInit ' Initialize USART


i var byte
i=0
'
Lcdout $fe, 128, "Counters: ", #i
pause 20
Main:
TMR0L = 0 ' Clear TMR0 count before start
USBService ' Service USB regularly


Loop1:
WHILE TMR0L = 0 ' Wait for high-to-low transition
WEND ' on RA4/T0CKI
pause 10
PORTB.0 = 1 ' LED on to indicate transition seen
i=i+1 ' Clear screen
buffer = i
Lcdout $fe, 128, "Counter: ", #i
'PAUSE 30
PORTB.0 = 0 ' LED off
USBService
USBOut 3, Buffer, cnt, Main

END

But I got this message "USB device not recognize". What could be causing this problem?

regards,
tacbanon

HenrikOlsson
- 6th October 2011, 06:13
Hi,
I hope someone else will chime in and try to help you because I've never used USB and don't know what the usual culprits are. Only advice I can give is to look at other examples and compare. Perhaps start with a working example and then add your counting code in "on top".

/Henrik.

tacbanon
- 6th October 2011, 09:26
Okay thanks for the advice Henrik

regards,
tacbanon

tacbanon
- 6th October 2011, 13:04
Hi everyone, now the usb device is recognized and its in Port COM2. My only problem is that my pulse counter variable i is reseting back to zero when a pulse(acctually I'm using a button on RA4 to simulate the pulse) is detected. In the code I did not yet insert the command "USBOut 3, buffer, cnt, Main" to send to pc.

asm ;__CONFIG _CONFIG1L, _PLLDIV_5_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
__CONFIG _CONFIG1L, _PLLDIV_2_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
__CONFIG _CONFIG1H, _FOSC_XTPLL_XT_1H & _FCMEN_OFF_1H & _IESO_OFF_1H
; ; ; Oscillator Switchover mode disabled
; ; Fail-Safe Clock Monitor disabled
; XT oscillator, PLL enabled, XT used by USB
__CONFIG _CONFIG2L, _PWRT_ON_2L & _BOR_ON_2L & _BORV_2_2L & _VREGEN_ON_2L
__CONFIG _CONFIG2H, _WDT_OFF_2H
__CONFIG _CONFIG3H, _MCLRE_ON_3H & _LPT1OSC_OFF_3H & _PBADEN_OFF_3H & _CCP2MX_ON_3H
__CONFIG _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _ICPRT_OFF_4L & _XINST_OFF_4L & _DEBUG_OFF_4L
endasm
DEFINE OSC 48
DEFINE LCD_DREG PORTB
DEFINE LCD_DBIT 0
DEFINE LCD_EREG PORTB
DEFINE LCD_EBIT 5
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBIT 4
DEFINE LCD_BITS 4
DEFINE LCD_LINES 2
DEFINE LCD_COMMANDUS 2000
DEFINE LCD_DATAUS 50
PAUSE 100


Include "cdc_desc.bas" ' Include the HID descriptors
buffer Var Byte[16]
Cnt VAR BYTE
Cnt = 2



TRISA.4 = 1 ' RA4/T0CKI = input to TMR0 counter
TRISA.0 = 0 ' RA0 = output for LED
CMCON = 7 ' All digital
ADCON1 = 001111 ' A/D converter off


'If you prefer, then increment on low-to-high transitions
T0CON = 111000
USBInit ' Initialize USART


i var byte
i=0
' Assign prescaler to WDT for 1:1 prescale on TMR0
' TMR0 clock will be from your external input on RA4/T0CKI
' Increment on high-to-low transitions


'
Lcdout $fe, 128, "Counter: ", #i
'pause 20
Main:
USBService ' Must service USB regularly
TMR0L = 0 ' Clear TMR0 count before start

Loop1:

WHILE TMR0L = 0 ' Wait for high-to-low transition
USBService
WEND ' on RA4/T0CKI
pause 5
PORTB.0 = 1 ' LED on to indicate transition seen
i=i+1 ' increment i
Lcdout $fe, 128, "Counter: ", #i
USBService
PORTB.0 = 0 ' LED off
I appreciate any help...

thanks in advance,
tacbanon

HenrikOlsson
- 6th October 2011, 13:24
When you enter the Loop1 routine you sit at the WHILE-WEND loop waiting for a pulse. When a pulse comes along you run thru the rest of the routine and finally pull PortB.0 low....then what happens?

There's no GOTO or END or anything after the Loop1 routine so the program will continue and continue and continue thru the (empty) codespace untill it reaches the end, then it'll wrap around and start over at the beginning and what happens there? Yep - you reset the count (i = 0) ;-)

By the way, you're now incrementing and displaying the i variable and not the TMR0 count but I guess that's intentional for now?

/Henrik.

tacbanon
- 6th October 2011, 22:53
Hi, yes your'e right Henrik "Goto Main" was all it need. :)

By the way, you're now incrementing and displaying the i variable and not the TMR0 count but I guess that's intentional for now?
I think TMR0 and variable i are the same but only not reseting to zero, and I need it for visual.

I hope they can allow me here to continue, because I'm planning to incorporate an SD card module to hold the number of coins. :o

regards,
tacbanon

tacbanon
- 7th October 2011, 14:13
Hi, I would like to incorporate an sdcard to the project and record the number of coins, only when pressing a button (e.g RB7) , but first I want to make a seperate test program using Pic18F4550. As I understood the connection should be simple enough but just to make sure I attached an image of my connections(Please correct me if it's wrong). I found a source code but it's in C.

// example writing to SD card, sford
#include "mbed.h"
#include "SDFileSystem.h"
SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board

int main() {
printf("Hello World!\n");

mkdir("/sd/mydir", 0777);

FILE *fp = fopen("/sd/mydir/sdtest.txt", "w");
if(fp == NULL) {
error("Could not open file for write\n");
}
fprintf(fp, "Hello fun SD Card World!");
fclose(fp);


printf("Goodbye World!\n");
}

The code creates a folder and writes "Hello fun SD Card World" to file name "sdtest.txt".
Hope anyone can help...

thanks in advance,
tacbanon

HenrikOlsson
- 7th October 2011, 14:50
Hi,
That C-example does nothing for or with PBP, the C-compiler used in the example (which ever it is) has a built in library function to support a SD-card. There is no such function built into PBP. HOWEVER there is a SD-card example on (who would've thought) the examples page (http://melabs.com/samples/PBP-mixed/index.htm) at MELABS (SDFS3.pbp).

With that said do you really need a SD-card? How much info do you need to store? The 4550 has 256bytes of EEPROM, can't you use that? Not that having a SD-card is "impossible" but using the EEPROM is going to be MUCH (I mean MUCH) easier than - which I think you'll see once you look at the example code.

/Henrik.

tacbanon
- 7th October 2011, 16:43
Hi Henrik, thanks for the responce. To be honest I already saw the link, but I'm skeptical because I dont really understand how it works(not simple for me). But I'm willing to try it out. I need to incorporate the sdcard because I'm trying to create a "coin operated computer rental app" I want later to save the number of coins and date/time per transaction. I also want to learn the EEPROM stuff but I was thinking about the capacity and since SDCARD can be quite cool (if I manage to incorporate it). I will try the link and post the results.

regards,
tacbanon

mackrackit
- 7th October 2011, 18:35
Maybe you can get some use from this?
http://www.picbasic.co.uk/forum/content.php?r=272-USB-SD-LOGGING

tacbanon
- 7th October 2011, 22:51
@mackrackit
very nice thanks for the link.

tacbanon
- 10th October 2011, 15:40
@mackrackit
Hi, I was able to run the example from melabs...but I'm interested how you made it as usb cdc and incorporating DT's interrupt, I tried to copy and paste your codes to see if it will compile, but unfortunately compiler errors occured on my setup. I have some questions that will help me understand using your code.
1. I use pic18F4550 instead of pic18F2550.
2. I dont have "SDFS.BAS" only "SDFS.PBP"
I dont have an rtc and temp sensor right now, only the sdcard. I commented out the functions that regards to the rtc and temp sensor. My goal at the meantime is to read/write to an sdcard and display its content to the hyperterminal.

thanks in advance,
tacbanon

mackrackit
- 10th October 2011, 15:54
The 4550 and 2550 are pretty much the same, just more or less I/Os.

SDFS.bas is the same as SDFS.pbp. Just different extensions. Way back when it was .bas.

What are the errors?

What version of PBP are you using?

Do you have DTs instant interrupt routines downloaded?

tacbanon
- 11th October 2011, 00:10
Hi, I was able to compile it without any error...and usb device is on port com2. I was expecting something will display on the communication terminal but I dont see any output. Probably I miss something on the conditions that needed to show some text.

'<FL_PIC18F2550>' '<FL_PBPL>'
DEFINE OSC 48
@ __CONFIG _CONFIG1L, _PLLDIV_5_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L ' I Change this, using 20Mhz crystal
@ __CONFIG _CONFIG1H, _FOSC_HSPLL_HS_1H
@ __CONFIG _CONFIG2H, _WDT_OFF_2H & _WDTPS_512_2H
@ __CONFIG _CONFIG2L, _PWRT_ON_2L & _VREGEN_ON_2L
@ __CONFIG _CONFIG3H, _PBADEN_OFF_3H & _MCLRE_OFF_3H
@ __CONFIG _CONFIG4L, _LVP_OFF_4L & _XINST_OFF_4L


' Alias PIC pins and registers for SD/MMC card ' I follow this connections
SD_WE VAR PORTA.4 ' SD card write protect
SD_WE_TRIS VAR TRISA.4 ' SD card write protect direction
SDI VAR PORTA.5 ' SPI data in SD #7
SDI_TRIS VAR TRISA.5 ' SPI data in direction
SCL VAR PORTA.3 ' SPI clock SD #5
SCL_TRIS VAR TRISA.3 ' SPI clock direction
SD_CS VAR PORTC.2 ' SD card chip select SD #1
SD_CS_TRIS VAR TRISC.2 ' SD card chip select direction
SD_CD VAR PORTC.0 ' SD card detect
SD_CD_TRIS VAR TRISC.0 ' SD card detect direction
SDO VAR PORTC.1 ' SPI data out SD #2
SDO_TRIS VAR TRISC.1 ' SPI data out direction
INCLUDE "SDFS.PBP" ' Change extension to PBP
SDC_UseHardSPI = FALSE ' Use hardware SSP port for SPI.
INCLUDE "cdc_desc.bas" ' I have CDC_Desc, DT_INTS-18 and ReEnterPBP.BAS in the same folder
INCLUDE "DT_INTS-18.bas"
INCLUDE "ReEnterPBP-18.bas"
ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
INT_Handler INT_INT, _CNT_PLUS, PBP, yes
INT_Handler USB_INT, _SERVICE_USB, ASM, yes
endm
INT_CREATE ; Creates the interrupt processor
ENDASM
'#########
'START USB AND INTERRUPTS
PAUSE 100 'TIME TO SETTLE
USBINIT 'INITIALIZE USB
USBSERVICE 'SERVICE USB
UIE = $7F 'ENABLE USB INTERRUPTS
UEIE = $9F 'ENABLE USB ERROR INTERRUPTS
PAUSE 500 'MORE TIME
USBINIT
USBSERVICE
@ INT_ENABLE USB_INT
USBSERVICE
@ INT_ENABLE INT_INT
'#########
'VARs AND DEFINEs
ADCON1 = 001110
LED VAR PORTA.2
TEXT_TIME VAR BYTE[6]
TEXT_DATE VAR BYTE[6]
TEXT_TEMP VAR BYTE[8]
TEXT_NO_CARD VAR BYTE[9]
MAC_FileName VAR BYTE[11]
B0 VAR BYTE
B1 VAR BYTE
CNT VAR WORD
CIU VAR BYTE 'CARD IN USE
CRON VAR BYTE[11]
CRON_D VAR BYTE[11]
SPACE VAR BYTE[2]
T_BUFFER VAR BYTE[8]
T_ONES VAR BYTE
T_TENS VAR BYTE
T_HUNS VAR BYTE
OUT_TEMP VAR BYTE
ADC_TEMP VAR WORD
S_TEMP VAR BYTE
X_TEMP VAR BYTE
'RTC DEFINES
DS_SCL VAR PORTB.1 'CLOCK
DS_SDA VAR PORTB.2 'DATA
RTC CON 010000
SEC_REG CON $00
CONT_REG CON $0E
CNTRL CON 000000
'RTC VARS
sec VAR BYTE: mins VAR BYTE: hr VAR BYTE: day VAR BYTE
date VAR BYTE: mon VAR BYTE: yr VAR BYTE
'RTC DEC VARS
SEC_O VAR BYTE: SEC_T VAR BYTE: MIN_O VAR BYTE: MIN_T VAR BYTE
HR_O VAR BYTE: HR_T VAR BYTE: MON_O VAR BYTE: MON_T VAR BYTE
DATE_O VAR BYTE: DATE_T VAR BYTE: YR_O VAR BYTE: YR_T VAR BYTE
'SD CARD FILE
FILE_seconds VAR BYTE: FILE_minutes VAR BYTE: FILE_hours VAR BYTE
FILE_day VAR BYTE: FILE_month VAR BYTE: FILE_year VAR BYTE
'#########
'SETS THE RTC TO PULSE OUT AT 1HZ
I2CWRITE DS_SDA, DS_SCL, RTC, CONT_REG, [CNTRL]
'SETS FILE NAME TO THE TIME BOARD IS POWERED
'GOSUB READ_RTC
MAC_FileName[0] = "1" '$30+HR_T 'Just made some dummy filename
MAC_FileName[1] = "0" '$30+HR_O
MAC_FileName[2] = "3" '$30+MIN_T
MAC_FileName[3] = "0" '$30+MIN_O
MAC_FileName[4] = "1" '$30+SEC_T
MAC_FileName[5] = "0" '$30+SEC_O
MAC_FileName[6] = " "
MAC_FileName[7] = " "
MAC_FileName[8] = "T"
MAC_FileName[9] = "X"
MAC_FileName[10] = "T"
'BUILD SOME USB TEXT
FOR B0 = 0 TO 5
LOOKUP B0,[" TIME "],B1
TEXT_TIME(B0) = B1
NEXT B0
FOR B0 = 0 TO 5
LOOKUP B0,[" DATE "],B1
TEXT_DATE(B0) = B1
NEXT B0
FOR B0 = 0 TO 7
LOOKUP B0,[" TEMP F "],B1
TEXT_TEMP(B0) = B1
NEXT B0
SPACE[0] = $d
SPACE[1] = $a
FOR B0 = 0 TO 9
LOOKUP B0,[" NO CARD",$d,$a],B1
TEXT_NO_CARD(B0) = B1
NEXT B0
CNT = 60 'I set CNT to 60 for test
'#########
'IF THE RTC NEEDS SET GOTO THE SET_RTC ROUTINE AND
'ENTER THE TIME AND DATE. RUN THE CODE ONCE THEN RE-COMMENT.
'GOSUB SET_RTC
'#########
'MAIN PROGRAM
CHECK:
IF CNT >= 60 THEN 'NUMBER OF SECONDS
CNT = 0
'GOSUB GET_T ' I disable this line, not read temperature
'GOSUB READ_RTC ' I disable this line, not read rtc
GOSUB USB_DISPLAY


IF (SD_WE = 0) AND (SD_CD = 0) THEN
GOTO SD_WRITE
ELSE
PWM LED,25,250
USBOUT 3, TEXT_NO_CARD, 10, CHECK
PAUSE 500
ENDIF
ENDIF
GOTO CHECK
'#########
'#########
'ISRs
CNT_PLUS:
IF CIU = 1 THEN
PWM LED,75,250
ELSE
IF (SD_WE = 0) AND (SD_CD = 0) THEN
TOGGLE LED
ELSE
LOW LED
ENDIF
ENDIF
CNT = CNT + 1
@ INT_RETURN


SERVICE_USB:
USBSERVICE
@ INT_RETURN
'#########
SET_RTC:
yr = $10
mon = $10
date = $09
sec = $00
mins = $52
hr = $02
I2CWRITE DS_SDA, DS_SCL, RTC, SEC_REG, [sec,mins,hr,day,date,mon,yr]
RETURN
'#########
READ_RTC:
I2CREAD DS_SDA, DS_SCL, RTC, SEC_REG, [sec,mins,hr,day,date,mon,yr]


SEC_T = sec & $70
SEC_T = SEC_T>>4
SEC_O = sec & $0F


MIN_T = mins & $70
MIN_T = MIN_T>>4
MIN_O = MINs & $0F


HR_T = hr & $70
HR_T = HR_T>>4
HR_O = hr & $0F


MON_T = mon & $70
MON_T = MON_T>>4
MON_O = mon & $0F


DATE_T = date & $70
DATE_T = DATE_T>>4
DATE_O = date & $0F


YR_T = yr & $70
YR_T = YR_T>>4
YR_O = yr & $0F


CRON[0] = " "
CRON[1] = $30+HR_T
CRON[2] = $30+HR_O
CRON[3] = ":"
CRON[4] = $30+MIN_T
CRON[5] = $30+MIN_O
CRON[6] = ":"
CRON[7] = $30+SEC_T
CRON[8] = $30+SEC_O
CRON[9] = $d
CRON[10] = $a


CRON_D[0] = " "
CRON_D[1] = $30+MON_T
CRON_D[2] = $30+MON_O
CRON_D[3] = "/"
CRON_D[4] = $30+DATE_T
CRON_D[5] = $30+DATE_O
CRON_D[6] = "/"
CRON_D[7] = $30+YR_T
CRON_D[8] = $30+YR_O
CRON_D[9] = $d
CRON_D[10] = $a


FILE_seconds = (SEC_T*10)+SEC_O
FILE_minutes = (MIN_T*10)+MIN_O
FILE_hours = (HR_T*10)+HR_O
FILE_day = (DATE_T*10)+DATE_O
FILE_month = (MON_T*10)+MON_O
FILE_year = (YR_T*10)+YR_O
RETURN
'#########
GET_T:
ADC_TEMP = 0
FOR X_TEMP = 1 TO 20
ADCON0=00000001
GOSUB READ_AD
S_TEMP = ADRESH
ADC_TEMP = ADC_TEMP + S_TEMP
PAUSE 250
NEXT X_TEMP
OUT_TEMP = ADC_TEMP / 20
OUT_TEMP = OUT_TEMP * 13/10
T_HUNS = OUT_TEMP/100
T_TENS = (OUT_TEMP - T_HUNS * 100)/10
T_ONES = OUT_TEMP-((T_HUNS*100)+(T_TENS*10))
T_BUFFER[0] = $30+T_HUNS
T_BUFFER[1] = $30+T_TENS
T_BUFFER[2] = $30+T_ONES
T_BUFFER[3] = " "
T_BUFFER[4] = " "
T_BUFFER[5] = " "
T_BUFFER[6] = $d
T_BUFFER[7] = $a
RETURN


READ_AD:
PAUSE 50
ADCON0.1=1
WHILE ADCON0.2=1:WEND
RETURN
'#########
USB_DISPLAY:
PAUSE 1
USBOUT 3, SPACE, 2, CHECK
PAUSE 1
USBOUT 3, TEXT_TIME, 6, CHECK
PAUSE 1
USBOUT 3, CRON, 11, CHECK
PAUSE 1
USBOUT 3, TEXT_DATE, 6, CHECK
PAUSE 1
USBOUT 3, CRON_D, 11, CHECK
PAUSE 1
USBOUT 3, TEXT_TEMP, 8, CHECK
PAUSE 1
USBOUT 3, T_BUFFER, 8, CHECK
PAUSE 1
RETURN
'#########
'SD CARD ROUTINES
SD_WRITE:
CIU = 1
SDINIT:
' FSInit initializes the card and reads all the preliminary information from it
GOSUB FSInit
IF (FAT_error != 0) THEN STOP


' Display card directory
GOSUB FINDfirst ' Find first file on card
WHILE (FAT_error = 0)
GOSUB FINDnext ' Find next file on card
WEND


SDFILENAME:
' This section defines a specific short (8.3) filename
' Note that spaces are use in empty elements and must be upper case for Windows
FAT_FileName[0] = MAC_FileName[0]
FAT_FileName[1] = MAC_FileName[1]
FAT_FileName[2] = MAC_FileName[2]
FAT_FileName[3] = MAC_FileName[3]
FAT_FileName[4] = MAC_FileName[4]
FAT_FileName[5] = MAC_FileName[5]
FAT_FileName[6] = MAC_FileName[6]
FAT_FileName[7] = MAC_FileName[7]
FAT_FileName[8] = MAC_FileName[8]
FAT_FileName[9] = MAC_FileName[9]
FAT_FileName[10] = MAC_FileName[10]


FAT_seconds = FILE_seconds
FAT_minutes = FILE_minutes
FAT_hours = FILE_hours
FAT_day = FILE_day
FAT_month = FILE_month
FAT_year = FILE_year+20


SDOPEN_W:
' Open a file for write
FAT_mode = "A" ' Write mode APPEND
GOSUB FSfopen ' Open file pointed to by Byte array FAT_FileName
IF (FAT_error = 10) THEN STOP


SD_WRITE_FILE:
' Write to file
FAT_src[0] = "T"
FAT_src[1] = "I"
FAT_src[2] = "M"
FAT_src[3] = "E"
FAT_src[4] = $d
FAT_src[5] = $a
FAT_src[6] = $30+HR_T
FAT_src[7] = $30+HR_O
FAT_src[8] = ":"
FAT_src[9] = $30+MIN_T
FAT_src[10] = $30+MIN_O
FAT_src[11] = ":"
FAT_src[12] = $30+SEC_T
FAT_src[13] = $30+SEC_O
FAT_src[14] = $d
FAT_src[15] = $a
FAT_src[16] = "D"
FAT_src[17] = "A"
FAT_src[18] = "T"
FAT_src[19] = "E"
FAT_src[20] = $d
FAT_src[21] = $a
FAT_src[22] = $30+MON_T
FAT_src[23] = $30+MON_O
FAT_src[24] = ":"
FAT_src[25] = $30+DATE_T
FAT_src[26] = $30+DATE_O
FAT_src[27] = ":"
FAT_src[28] = $30+YR_T
FAT_src[29] = $30+YR_O
FAT_src[30] = $d
FAT_src[31] = $a
FAT_src[32] = " "
FAT_src[33] = $30+T_HUNS
FAT_src[34] = $30+T_TENS
FAT_src[35] = $30+T_ONES
FAT_src[36] = " "
FAT_src[37] = "F"
FAT_src[38] = $d
FAT_src[39] = $a
FAT_src[40] = $d
FAT_src[41] = $a
FAT_count = 42
GOSUB FSfwrite


IF (FAT_error = 10) THEN STOP
IF (FAT_error != 0) THEN STOP


SDCLOSE:
' Close file
GOSUB FSfclose
IF (FAT_error != 0) THEN STOP
PAUSE 5000
CIU = 0
GOTO CHECK
What do you think I'm missing?

thanks,
tacbanon

mackrackit
- 11th October 2011, 08:40
Lets test the USB.
Go here (http://www.picbasic.co.uk/forum/showthread.php?t=11728&p=78306#post78306) and grab "USB_ASM_Service.pbp"

Then try this.


INCLUDE "cdc_desc.bas" ' Include the HID descriptors
INCLUDE "USB_ASM_Service.pbp" ' Base Interrupt System


BUFFER VAR BYTE[14]
LED VAR PORTA.2
ADCON1 = %00001111
CMCON = 7

buffer[0] = "H"
buffer[1] = "e"
buffer[2] = "l"
buffer[3] = "l"
buffer[4] = "o"
buffer[5] = " "
buffer[6] = "W"
buffer[7] = "o"
buffer[8] = "r"
buffer[9] = "l"
buffer[10] = "d"
buffer[11] = 13
buffer[12] = 10
buffer[13] = 0

Loop1:
USBOUT 3, buffer, 14, loop1
pause 500
TOGGLE LED
GOTO loop1
END


What version of PBP are you using?

tacbanon
- 11th October 2011, 12:06
Hi, I did test your code and its running without no problem.
I'm using PBP2.60

regards,
tacbanon

mackrackit
- 12th October 2011, 08:28
Ok, now we know your USB hardware is working. Have you been able to write anything to the SD card? Try a small SD card program without USB to make sure the SD card stuff works.

tacbanon
- 12th October 2011, 13:19
Hi, thanks for the responce. I successfully run this sample sd program.

asm __CONFIG _CONFIG1L, _PLLDIV_5_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
__CONFIG _CONFIG2L, _PWRT_ON_2L & _BOR_ON_2L & _BORV_2_2L & _VREGEN_ON_2L
__CONFIG _CONFIG2H, _WDT_OFF_2H
__CONFIG _CONFIG3H, _MCLRE_ON_3H & _LPT1OSC_OFF_3H & _PBADEN_OFF_3H & _CCP2MX_ON_3H
__CONFIG _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _ICPRT_OFF_4L & _XINST_OFF_4L & _DEBUG_OFF_4L
endasm
DEFINE OSC 48
' Name : sdfs3.pbp
' Compiler : PICBASIC PRO Compiler 2.6 (PBPL only)
' Assembler : MPASM
' Target PIC : PIC16F4550
' Hardware : Schematic available in http://melabs.com/resources/samples/pbp/sdfs3.zip
' Oscillator : 20MHz
' Keywords : SD Card, SD/MMC, FAT16
' Description : 10/02/08 - PICBASIC PRO 2.50L test program to talk to FAT16
' formatted MMC/SD cards with PIC18F4550. This is only a test program that
' is part of a larger project. The complete fileset with schematic, includes,
' and details can be found at download:
' http://melabs.com/resources/samples/pbp/sdfs3.zip




' Alias PIC pins and registers for SD/MMC card
SD_WE Var PORTA.4 ' SD card write protect
SD_WE_TRIS Var TRISA.4 ' SD card write protect direction
SDI Var PORTB.0 ' SPI data in
SDI_TRIS Var TRISB.0 ' SPI data in direction
SCL Var PORTB.1 ' SPI clock
SCL_TRIS Var TRISB.1 ' SPI clock direction
SD_CS Var PORTB.3 ' SD card chip select
SD_CS_TRIS Var TRISB.3 ' SD card chip select direction
SD_CD Var PORTB.4 ' SD card detect
SD_CD_TRIS Var TRISB.4 ' SD card detect direction




SDO Var PORTC.7 ' SPI data out
SDO_TRIS Var TRISC.7 ' SPI data out direction




' Include the SD/MMC subroutines (found in http://melabs.com/resources/samples/pbp/sdfs3.zip)
Include "SDFS.PBP"
SDC_UseHardSPI = TRUE ' Use hardware SSP port for SPI.


ADCON1 = 15 ' All I/O pins digital
Pause 100


' FSInit initializes the card and reads all the preliminary information from it
Gosub FSInit
Serout2 PORTC.6, 84, ["Init: ", Dec FAT_error, " ", Dec SDC_status, " ", Dec SDC_response, $d, $a]
If (FAT_error != 0) Then Stop


' Display card directory
Gosub FINDfirst ' Find first file on card
While (FAT_error = 0)
Serout2 PORTC.6, 84, [Str FAT_FileName\11, $d, $a]
Gosub FINDnext ' Find next file on card
Wend


' This section defines a specific short (8.3) filename
' Note that spaces are use in empty elements and must be upper case for Windows
FAT_FileName[0] = "T"
FAT_FileName[1] = "E"
FAT_FileName[2] = "S"
FAT_FileName[3] = "T"
FAT_FileName[4] = "1"
FAT_FileName[5] = " "
FAT_FileName[6] = " "
FAT_FileName[7] = " "
FAT_FileName[8] = "T"
FAT_FileName[9] = "X"
FAT_FileName[10] = "T"


' Set file time to 8:30:10 and date to 1/1/2008
FAT_seconds = 5
FAT_minutes = 30
FAT_hours = 8
FAT_day = 1
FAT_month = 1
FAT_year = 28


' Open a file for write
FAT_mode = "w" ' Write mode
Gosub FSfopen ' Open file pointed to by Byte array FAT_FileName
Serout2 PORTC.6, 84, ["Open for write: ", Dec FAT_error, $d, $a]
If (FAT_error != 0) Then Stop


' Write to file
FAT_src[0] = "A"
FAT_src[1] = "B"
FAT_src[2] = "C"
FAT_count = 3
Gosub FSfwrite
Serout2 PORTC.6, 84, [ "Write ", Dec FAT_error, $d, $a]
If (FAT_error != 0) Then Stop


' Close file
Gosub FSfclose
Serout2 PORTC.6, 84, [ "Close ", Dec FAT_error, $d, $a]
If (FAT_error != 0) Then Stop


' Open a file for read
FAT_mode = "r" ' Read mode
Gosub FSfopen ' Open file pointed to by Byte array FAT_FileName
Serout2 PORTC.6, 84, ["Open: ", Dec FAT_error, $d, $a]
If (FAT_error != 0) Then Stop


' Read and display the whole file
FAT_count = 1 ' Read 1 byte to buffer at a time
Gosub FSfread
While (FAT_error = 0)
Serout2 PORTC.6, 84, [FAT_dest[0]]
FAT_count = 1 ' Read 1 byte to buffer at a time
Gosub FSfread
Wend
Serout2 PORTC.6, 84, [ "Read: ", Dec FAT_error, $d, $a]
End



I also have my rtc module available. but at this time I do know how to use your code or connect it the developmemt board.

regards,
tacbanon

tacbanon
- 12th October 2011, 15:14
Hi, I was able to test my RTC from this thread http://www.picbasic.co.uk/forum/showthread.php?t=12918...so far so good :)

regards,
tacbanon

mackrackit
- 12th October 2011, 15:40
COOL!!!!! :)

The next piece of the puzzle is to get familiar with DT's instant interrupts. If you have not already done so.
The short USB example earlier will need to be re-written to use DT's instants in place of the USB service include.

Then put all of the pieces together. Modular type programming....

tacbanon
- 16th October 2011, 06:06
Hi, sorry this question is related to previous post #27...I need to to send a character "c"(dec 99) constantly to the pc in the main loop(loop1). Then send 1(dec 49) if any coin pulse is detected.

' Main Program LoopLoop1:
'Lcdout $fe, 128, "Counter: ", #i
USBService ' Must service USB regularly
'**************ADDED COde
Buffer2[0] = 99
Buffer2[1] =13
TMR0L = 0
USBOut 3, Buffer2, cnt,Loop1
goto Loop1
'****************************
TMR0L = 0 ' Clear TMR0 count before start

Loop2:
USBService
WHILE TMR0L = 0 ' Wait for high-to-low transition
USBService
WEND ' on RA4/T0CKI
pause 5
PORTB.0 = 1 ' LED on to indicate transition seen
i=i+1 ' Clear screen
USBService
buffer[0] = 49
Buffer[1] = 13
USBOut 3, Buffer, cnt,Loop2
'Lcdout $fe, 128, "Counter: ", #i
'PAUSE 30
PORTB.0 = 0 ' LED off
goto Loop1
When I run the program..it displays "c" to the pc but never detect pulse when coin is dropped. Can you help me out what I'm doing wrong?

thanks,
tacbanon

mackrackit
- 16th October 2011, 06:33
Post the whole code.

tacbanon
- 16th October 2011, 06:57
Hi, here is the code...



' Name : Coin_Internet_CDC.pbp
' Compiler : PICBASIC PRO Compiler 2.6
' Assembler : PM or MPASM
' Target PIC : PIC18F4550
' Hardware : Easypic6 Experimenter Board
' Oscillator : 4MHz external crystal (only???)
' Thanks : To Henrik, and other PBP experts
' Description : PICBASIC PRO program to show virtual serial comm. Working


asm
__CONFIG _CONFIG1L, _PLLDIV_1_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
__CONFIG _CONFIG1H, _FOSC_HSPLL_HS_1H & _FCMEN_OFF_1H & _IESO_OFF_1H
__CONFIG _CONFIG2L, _PWRT_ON_2L & _BOR_ON_2L & _BORV_2_2L & _VREGEN_ON_2L
__CONFIG _CONFIG2H, _WDT_OFF_2H
__CONFIG _CONFIG3H, _MCLRE_ON_3H & _LPT1OSC_OFF_3H & _PBADEN_OFF_3H & _CCP2MX_ON_3H
__CONFIG _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _ICPRT_OFF_4L & _XINST_OFF_4L & _DEBUG_OFF_4L
endasm
DEFINE OSC 48
DEFINE LCD_DREG PORTB
DEFINE LCD_DBIT 0
DEFINE LCD_EREG PORTB
DEFINE LCD_EBIT 5
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBIT 4
DEFINE LCD_BITS 4
DEFINE LCD_LINES 2
DEFINE LCD_COMMANDUS 2000
DEFINE LCD_DATAUS 50
PAUSE 100
Include "cdc_desc.bas" ' Include the HID descriptors
Buffer VAR BYTE[10]
Buffer2 VAR BYTE[10]
Cnt VAR BYTE

i var byte

INTCON2.7 = 0 ' Enable PORTB pull-ups
TRISB = 110000 ' Enable all buttons
ADCON1 = 15 ' Set all I/Os to Digital


CMCON = 7 ' Disable Comparators
Cnt = 3
'************************************************* ***
TRISA.4 = 1 ' RA4/T0CKI = input to TMR0 counter
TRISA.0 = 0 ' RA0 = output for LED
CMCON = 7 ' All digital
ADCON1 = 001111 ' A/D converter off


'If you prefer, then increment on low-to-high transitions
T0CON = 111000


'************************************************* ****


USBInit ' Initialize USART


' Main Program Loop
Loop1:
Lcdout $fe, 128, "Counter: ", #i
USBService ' Must service USB regularly
'**************ADDED COde
Buffer2[0] = 99
Buffer2[1] =13
USBOut 3, Buffer2, cnt,Loop1
'goto Loop1
'****************************
TMR0L = 0 ' Clear TMR0 count before start


Loop2:
USBService
WHILE TMR0L = 0 ' Wait for high-to-low transition
USBService
WEND ' on RA4/T0CKI
pause 5
PORTB.0 = 1 ' LED on to indicate transition seen
i=i+1 ' Clear screen
USBService
buffer[0] = 49
Buffer[1] = 13
USBOut 3, Buffer, cnt,Loop2
Lcdout $fe, 128, "Counter: ", #i
'PAUSE 30
PORTB.0 = 0 ' LED off
goto Loop1


regards,
tacbanon

mackrackit
- 16th October 2011, 07:37
I see missing % on these

ADCON1 = 001111
T0CON = 111000

And each one should have 8 bits.

Go back and look at post #19 of this thread.

tacbanon
- 16th October 2011, 14:10
' Name : Coin_Internet_CDC.pbp' Compiler : PICBASIC PRO Compiler 2.6
' Assembler : PM or MPASM
' Target PIC : PIC18F4550
' Hardware : Easypic6 Experimenter Board
' Oscillator : 4MHz external crystal (only???)
' Thanks : To Henrik, and other PBP experts
' Description : PICBASIC PRO program to show virtual serial comm. Working


asm
__CONFIG _CONFIG1L, _PLLDIV_1_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
__CONFIG _CONFIG1H, _FOSC_HSPLL_HS_1H & _FCMEN_OFF_1H & _IESO_OFF_1H
__CONFIG _CONFIG2L, _PWRT_ON_2L & _BOR_ON_2L & _BORV_2_2L & _VREGEN_ON_2L
__CONFIG _CONFIG2H, _WDT_OFF_2H
__CONFIG _CONFIG3H, _MCLRE_ON_3H & _LPT1OSC_OFF_3H & _PBADEN_OFF_3H & _CCP2MX_ON_3H
__CONFIG _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _ICPRT_OFF_4L & _XINST_OFF_4L & _DEBUG_OFF_4L
endasm
DEFINE OSC 48
DEFINE LCD_DREG PORTB
DEFINE LCD_DBIT 0
DEFINE LCD_EREG PORTB
DEFINE LCD_EBIT 5
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBIT 4
DEFINE LCD_BITS 4
DEFINE LCD_LINES 2
DEFINE LCD_COMMANDUS 2000
DEFINE LCD_DATAUS 50
PAUSE 100
Include "cdc_desc.bas" ' Include the HID descriptors
Buffer VAR BYTE[10]
Buffer2 VAR BYTE[10]
Cnt VAR BYTE


b0 var byte
B1 VAR BYTE ' Working buffer 1 for button command
sel VAR BYTE ' Working buffer 2 for button command
B3 VAR BYTE ' Working buffer 3 for button command
i var byte
Clear ' Clear buffers


INTCON2.7 = 0 ' Enable PORTB pull-ups
TRISB = 110000 ' Enable all buttons
ADCON1 = 15 ' Set all I/Os to Digital


CMCON = 7 ' Disable Comparators
Cnt = 5
i = 0
'************************************************* ***
TRISA.4 = 1 ' RA4/T0CKI = input to TMR0 counter
TRISA.0 = 0 ' RA0 = output for LED
CMCON = 7 ' All digital


'If you prefer, then increment on low-to-high transitions
T0CON = %11111000


'************************************************* ****
USBInit ' Initialize USART
' Main Program Loop
Loop1:
Lcdout $fe, 128, "Counter: ", #i
USBService ' Must service USB regularly
TMR0L = 0 ' Clear TMR0 count before start
'**************ADDED COde
'gosub usbtx
'****************************
'goto Loop1


Loop2:
USBService
WHILE TMR0L = 0 ' Wait for high-to-low transition
USBService
WEND ' on RA4/T0CKI
pause 5
PORTB.0 = 1 ' LED on to indicate transition seen
i=i+1 ' Clear screen
USBService
buffer[0] = 49
Buffer[1] = 13
USBOut 3, Buffer, cnt,Loop2
Lcdout $fe, 128, "Counter: ", #i
'PAUSE 30
PORTB.0 = 0 ' LED off
goto Loop1
Hi mackrackit, the above code works. Now what I'm trying to do is add a ntoher line of code to send "c" in the main loop but I can not seem to make it work.


asm __CONFIG _CONFIG1L, _PLLDIV_1_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
__CONFIG _CONFIG1H, _FOSC_HSPLL_HS_1H & _FCMEN_OFF_1H & _IESO_OFF_1H
__CONFIG _CONFIG2L, _PWRT_ON_2L & _BOR_ON_2L & _BORV_2_2L & _VREGEN_ON_2L
__CONFIG _CONFIG2H, _WDT_OFF_2H
__CONFIG _CONFIG3H, _MCLRE_ON_3H & _LPT1OSC_OFF_3H & _PBADEN_OFF_3H & _CCP2MX_ON_3H
__CONFIG _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _ICPRT_OFF_4L & _XINST_OFF_4L & _DEBUG_OFF_4L
endasm
DEFINE OSC 48
DEFINE LCD_DREG PORTB
DEFINE LCD_DBIT 0
DEFINE LCD_EREG PORTB
DEFINE LCD_EBIT 5
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBIT 4
DEFINE LCD_BITS 4
DEFINE LCD_LINES 2
DEFINE LCD_COMMANDUS 2000
DEFINE LCD_DATAUS 50
PAUSE 100
Include "cdc_desc.bas" ' Include the HID descriptors
Buffer VAR BYTE[10]
Buffer2 VAR BYTE[10]
Cnt VAR BYTE


b0 var byte
B1 VAR BYTE ' Working buffer 1 for button command
sel VAR BYTE ' Working buffer 2 for button command
B3 VAR BYTE ' Working buffer 3 for button command
i var byte
Clear ' Clear buffers


INTCON2.7 = 0 ' Enable PORTB pull-ups
TRISB = 110000 ' Enable all buttons
ADCON1 = 15 ' Set all I/Os to Digital


CMCON = 7 ' Disable Comparators
Cnt = 5
i = 0
'************************************************* ***
TRISA.4 = 1 ' RA4/T0CKI = input to TMR0 counter
TRISA.0 = 0 ' RA0 = output for LED
CMCON = 7 ' All digital


'If you prefer, then increment on low-to-high transitions
T0CON = %11111000


'************************************************* ****


USBInit ' Initialize USART




' Main Program Loop
Loop1:
Lcdout $fe, 128, "Counter: ", #i
USBService ' Must service USB regularly
TMR0L = 0 ' Clear TMR0 count before start
'**************ADDED COde
gosub usbtx ' transmit character c to pc
'****************************
Loop2:
USBService
WHILE TMR0L = 0 ' Wait for high-to-low transition
USBService
WEND ' on RA4/T0CKI
pause 5
PORTB.0 = 1 ' LED on to indicate transition seen
i=i+1 ' Clear screen
USBService
buffer[0] = 49
Buffer[1] = 13
USBOut 3, Buffer, cnt,Loop2
Lcdout $fe, 128, "Counter: ", #i
'PAUSE 30
PORTB.0 = 0 ' LED off
goto Loop1

usbtx:
USBService
Buffer2[0] = 99
Buffer2[1] = 13
USBOut 3, Buffer2, cnt,usbtx
return
The above code I use to attempt sending to pc...BTW I dont know what happened (T0CON = %11111000 and ADCON1 = %00001111) to the bits (but I think I did not alter it).

thanks,
tacbanon

mackrackit
- 16th October 2011, 15:46
Does the Loop2 part still work in the code with the usbtx sub routine? It looks to me like it should work. Or maybe it is too long between USBservices.

If that is the case include the routine to service the USB in the background like the example gave for testing.

tacbanon
- 16th October 2011, 17:41
Hi, yes Loop2 still works(i increments), but I wonder why usbtx does not sends the "c" character to pc?

regards,
tacbanon

mackrackit
- 16th October 2011, 19:29
Give this a shot. It works here.



INCLUDE "cdc_desc.bas"
INCLUDE "USB_ASM_Service.pbp" ' Base Interrupt System


LED VAR PORTB.0
Buffer VAR BYTE[10]
Buffer2 VAR BYTE[10]
Cnt VAR BYTE


i var byte
ADCON1 = 15 ' Set all I/Os to Digital
CMCON = 7 ' Disable Comparators
Cnt = 5
i = 0
'************************************************* ***
TRISA.4 = 1 ' RA4/T0CKI = input to TMR0 counter
'If you prefer, then increment on low-to-high transitions
T0CON = 111000
LOW LED
Loop1:
TMR0L = 0 ' Clear TMR0 count before start
'**************ADDED COde
gosub usbtx ' transmit character c to pc
'****************************
Loop2:
WHILE TMR0L = 0 ' Wait for high-to-low transition
WEND ' on RA4/T0CKI
pause 5
PORTA.2 = 1 ' LED on to indicate transition seen LED PORTA.2
i=i+1 ' Clear screen
buffer[0] = 49
Buffer[1] = 13
USBOut 3, Buffer, 2,Loop2
LOW LED ' LED off
goto Loop1


usbtx:
TOGGLE LED
Buffer2[0] = 99
Buffer2[1] = 13
USBOut 3, Buffer2,2,usbtx
return

tacbanon
- 17th October 2011, 04:47
Hi, thanks for the time. I run your code...but the character "c" appears only when a pulse is detected in my setup. Hmm it should be running as expected...

regards,
tacbanon

mackrackit
- 17th October 2011, 13:35
That is what I thought you wanted?

If you want it to send "C" continuously


WHILE TMR0L = 0 ' Wait for high-to-low transition
GOSUB usbtx
PAUSE 100
WEND ' on RA4/T0CKI

tacbanon
- 18th October 2011, 22:28
Hi, sorry if was not making clear..it's working now:). acctually the reason I need to send "c" is to identify if the usb device is connected to the my pc application. My next step is to incorporate rtc and sdcards for record keeping. I will post my code later... thanks mackrackit.

regards,
tacbanon

mackrackit
- 19th October 2011, 09:08
glad I could help.

tacbanon
- 23rd October 2011, 13:18
Hi, sorry for the delay. I managed to send and display the content of the rtc to communication terminal using CDC.
Next I tried to incorporate the SDcard using macrackit's codes. As I understood the code, once powered it should write a filename(timestamp) to sdcard. But When I checked the sdcard there was none.
This is the code...

asm ;_PLLDIV_2_1L EQU H'F9' ; Divide by 2 (8 MHz oscillator input)

; __CONFIG _CONFIG1L, _PLLDIV_2_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
;__CONFIG _CONFIG1L, _PLLDIV_5_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
;__CONFIG _CONFIG1H, _FOSC_HSPLL_HS_1H & _FCMEN_OFF_1H & _IESO_OFF_1H
__CONFIG _CONFIG1L, _PLLDIV_1_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
; ; ; USB clock source comes from the 96 MHz PLL divided by 2
; ; [OSC1/OSC2 Src: /1][96 MHz PLL Src: /2]
; No prescale (4 MHz oscillator input drives PLL directly)




;__CONFIG _CONFIG1H, _FOSC_XTPLL_XT_1H & _FCMEN_OFF_1H & _IESO_OFF_1H '2
; ; ; Oscillator Switchover mode disabled
; ; Fail-Safe Clock Monitor disabled
; XT oscillator, PLL enabled, XT used by USB
__CONFIG _CONFIG2L, _PWRT_ON_2L & _BOR_ON_2L & _BORV_2_2L & _VREGEN_ON_2L
__CONFIG _CONFIG2H, _WDT_OFF_2H
__CONFIG _CONFIG3H, _MCLRE_ON_3H & _LPT1OSC_OFF_3H & _PBADEN_OFF_3H & _CCP2MX_ON_3H
__CONFIG _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _ICPRT_OFF_4L & _XINST_OFF_4L & _DEBUG_OFF_4L
endasm
DEFINE OSC 48
DEFINE LCD_DREG PORTB
DEFINE LCD_DBIT 0
DEFINE LCD_EREG PORTB
DEFINE LCD_EBIT 5
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBIT 4
DEFINE LCD_BITS 4
DEFINE LCD_LINES 2
DEFINE LCD_COMMANDUS 2000
DEFINE LCD_DATAUS 50


'RTC pins on 4550
'SDA Var PORTB.0
'SCL Var PORTB.1
' Alias PIC pins and registers for SD/MMC card
SD_WE VAR PORTA.4 ' SD card write protect
SD_WE_TRIS VAR TRISA.4 ' SD card write protect direction
SDI VAR PORTA.5 ' SPI data in SD #7
SDI_TRIS VAR TRISA.5 ' SPI data in direction
SCL VAR PORTA.3 ' SPI clock SD #5
SCL_TRIS VAR TRISA.3 ' SPI clock direction
SD_CS VAR PORTC.2 ' SD card chip select SD #1
SD_CS_TRIS VAR TRISC.2 ' SD card chip select direction
SD_CD VAR PORTC.0 ' SD card detect
SD_CD_TRIS VAR TRISC.0 ' SD card detect direction
SDO VAR PORTC.1 ' SPI data out SD #2
SDO_TRIS VAR TRISC.1 ' SPI data out direction
INCLUDE "SDFS.PBP"
SDC_UseHardSPI = FALSE ' Use hardware SSP port for SPI.
'################################################# #######################################3
INCLUDE "cdc_desc.bas"
INCLUDE "DT_INTS-18.bas"
INCLUDE "ReEnterPBP-18.bas"
ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
INT_Handler INT_INT, _CNT_PLUS, PBP, yes
INT_Handler USB_INT, _SERVICE_USB, ASM, yes
endm
INT_CREATE ; Creates the interrupt processor
ENDASM
'#########
'START USB AND INTERRUPTS
PAUSE 100 'TIME TO SETTLE
USBINIT 'INITIALIZE USB
USBSERVICE 'SERVICE USB
UIE = $7F 'ENABLE USB INTERRUPTS
UEIE = $9F 'ENABLE USB ERROR INTERRUPTS
PAUSE 500 'MORE TIME
USBINIT
USBSERVICE
@ INT_ENABLE USB_INT
'
'################################################# #########################################


ADCON1 = %00001110
LED VAR PORTA.2
TEXT_TIME VAR BYTE[6]
TEXT_DATE VAR BYTE[6]
CRON VAR BYTE[11]
CRON_D VAR BYTE[11]
TEXT_NO_CARD VAR BYTE[9]
MAC_FileName VAR BYTE[11]
T_BUFFER VAR BYTE[8]
T_ONES VAR BYTE
T_TENS VAR BYTE
T_HUNS VAR BYTE
B0 VAR BYTE
B1 VAR BYTE
CNT VAR WORD
CIU VAR BYTE 'CARD IN USE
'RTC DEFINES
DS_SCL VAR PORTB.1 'CLOCK
DS_SDA VAR PORTB.2 'DATA
RTC CON %11010000
SEC_REG CON $00
CONT_REG CON $0E
CNTRL CON %00000000
'################################################# #########################################
'RTC VARS
sec VAR BYTE: mins VAR BYTE: hr VAR BYTE: day VAR BYTE
date VAR BYTE: mon VAR BYTE: yr VAR BYTE
'RTC DEC VARS
SEC_O VAR BYTE: SEC_T VAR BYTE:
MIN_O VAR BYTE: MIN_T VAR BYTE
HR_O VAR BYTE: HR_T VAR BYTE:
MON_O VAR BYTE: MON_T VAR BYTE
DATE_O VAR BYTE: DATE_T VAR BYTE:
YR_O VAR BYTE: YR_T VAR BYTE
'SD CARD FILE
FILE_seconds VAR BYTE: FILE_minutes VAR BYTE: FILE_hours VAR BYTE
FILE_day VAR BYTE: FILE_month VAR BYTE: FILE_year VAR BYTE
'#########
'SETS FILE NAME TO THE TIME BOARD IS POWERED
GOSUB READ_RTC
MAC_FileName[0] = $30+HR_T
MAC_FileName[1] = $30+HR_O
MAC_FileName[2] = $30+MIN_T
MAC_FileName[3] = $30+MIN_O
MAC_FileName[4] = $30+SEC_T
MAC_FileName[5] = $30+SEC_O
MAC_FileName[6] = " "
MAC_FileName[7] = " "
MAC_FileName[8] = "T"
MAC_FileName[9] = "X"
MAC_FileName[10] = "T"


'################################################# #########################################


FOR B0 = 0 TO 5
LOOKUP B0,[" TIME "],B1
TEXT_TIME(B0) = B1
NEXT B0
FOR B0 = 0 TO 5
LOOKUP B0,[" DATE "],B1
TEXT_DATE(B0) = B1
NEXT B0
FOR B0 = 0 TO 9
LOOKUP B0,[" NO CARD",$d,$a],B1
TEXT_NO_CARD(B0) = B1
NEXT B0
'################################################# #########################################


CMCON = %00000111 ' Comparators = off
ADCON1 = %00001111 ' A/D converter off
' Initialize LCD
LCDOUT $FE,1:FLAGS=0:PAUSE 250:LCDOUT $FE,1:PAUSE 250


Goto mainloop ' Skip over subroutines




'ISRs
CNT_PLUS:
IF CIU = 1 THEN
PWM LED,75,250
ELSE
IF (SD_WE = 0) AND (SD_CD = 0) THEN
TOGGLE LED
ELSE
LOW LED
ENDIF
ENDIF
CNT = CNT + 1
@ INT_RETURN


'#########
' Subroutine to read time from RTC
READ_RTC:
I2CREAD DS_SDA, DS_SCL, RTC, SEC_REG, [sec,mins,hr,day,date,mon,yr]
SEC_T = sec & $70
SEC_T = SEC_T>>4
SEC_O = sec & $0F


MIN_T = mins & $70
MIN_T = MIN_T>>4
MIN_O = MINs & $0F


HR_T = hr & $70
HR_T = HR_T>>4
HR_O = hr & $0F


MON_T = mon & $70
MON_T = MON_T>>4
MON_O = mon & $0F


DATE_T = date & $70
DATE_T = DATE_T>>4
DATE_O = date & $0F


YR_T = yr & $70
YR_T = YR_T>>4
YR_O = yr & $0F


CRON[0] = " "
CRON[1] = $30+HR_T
CRON[2] = $30+HR_O
CRON[3] = ":"
CRON[4] = $30+MIN_T
CRON[5] = $30+MIN_O
CRON[6] = ":"
CRON[7] = $30+SEC_T
CRON[8] = $30+SEC_O
CRON[9] = $d
CRON[10] = $a


CRON_D[0] = " "
CRON_D[1] = $30+MON_T
CRON_D[2] = $30+MON_O
CRON_D[3] = "/"
CRON_D[4] = $30+DATE_T
CRON_D[5] = $30+DATE_O
CRON_D[6] = "/"
CRON_D[7] = $30+YR_T
CRON_D[8] = $30+YR_O
CRON_D[9] = $d
CRON_D[10] = $a

FILE_seconds = (SEC_T*10)+SEC_O
FILE_minutes = (MIN_T*10)+MIN_O
FILE_hours = (HR_T*10)+HR_O
FILE_day = (DATE_T*10)+DATE_O
FILE_month = (MON_T*10)+MON_O
FILE_year = (YR_T*10)+YR_O
Return


mainloop:

Gosub READ_RTC ' Read the time from the RTC


' Display time on LCD
Lcdout $fe, 128,"Date:", CRON_D[1],CRON_D[2],CRON_D[3],CRON_D[4],CRON_D[5],CRON_D[6],CRON_D[7],CRON_D[8]
Lcdout $fe, $c0, "Time:", CRON[1], CRON[2],CRON[3],CRON[4],CRON[5],CRON[6],CRON[7],CRON[8]
Pause 500
GOSUB USB_DISPLAY
Goto mainloop




'#########
USB_DISPLAY:
USBOUT 3, TEXT_TIME, 6, mainloop
PAUSE 1
USBOUT 3, CRON, 11, mainloop
PAUSE 1
USBOUT 3, TEXT_DATE, 6, mainloop
PAUSE 1
USBOUT 3, CRON_D, 11, mainloop
PAUSE 1


RETURN
'#########


'####################


@ INT_RETURN


SERVICE_USB:
USBSERVICE
@ INT_RETURN
'#########


'SD CARD ROUTINES
SD_WRITE:
CIU = 1
SDINIT:
' FSInit initializes the card and reads all the preliminary information from it
GOSUB FSInit
IF (FAT_error != 0) THEN STOP


' Display card directory
GOSUB FINDfirst ' Find first file on card
WHILE (FAT_error = 0)
GOSUB FINDnext ' Find next file on card
WEND


SDFILENAME:
' This section defines a specific short (8.3) filename
' Note that spaces are use in empty elements and must be upper case for Windows
FAT_FileName[0] = MAC_FileName[0]
FAT_FileName[1] = MAC_FileName[1]
FAT_FileName[2] = MAC_FileName[2]
FAT_FileName[3] = MAC_FileName[3]
FAT_FileName[4] = MAC_FileName[4]
FAT_FileName[5] = MAC_FileName[5]
FAT_FileName[6] = MAC_FileName[6]
FAT_FileName[7] = MAC_FileName[7]
FAT_FileName[8] = MAC_FileName[8]
FAT_FileName[9] = MAC_FileName[9]
FAT_FileName[10] = MAC_FileName[10]


FAT_seconds = FILE_seconds
FAT_minutes = FILE_minutes
FAT_hours = FILE_hours
FAT_day = FILE_day
FAT_month = FILE_month
FAT_year = FILE_year+20


SDOPEN_W:
' Open a file for write
FAT_mode = "A" ' Write mode APPEND
GOSUB FSfopen ' Open file pointed to by Byte array FAT_FileName
IF (FAT_error = 10) THEN STOP


SD_WRITE_FILE:
' Write to file
FAT_src[0] = "T"
FAT_src[1] = "I"
FAT_src[2] = "M"
FAT_src[3] = "E"
FAT_src[4] = $d
FAT_src[5] = $a
FAT_src[6] = $30+HR_T
FAT_src[7] = $30+HR_O
FAT_src[8] = ":"
FAT_src[9] = $30+MIN_T
FAT_src[10] = $30+MIN_O
FAT_src[11] = ":"
FAT_src[12] = $30+SEC_T
FAT_src[13] = $30+SEC_O
FAT_src[14] = $d
FAT_src[15] = $a
FAT_src[16] = "D"
FAT_src[17] = "A"
FAT_src[18] = "T"
FAT_src[19] = "E"
FAT_src[20] = $d
FAT_src[21] = $a
FAT_src[22] = $30+MON_T
FAT_src[23] = $30+MON_O
FAT_src[24] = ":"
FAT_src[25] = $30+DATE_T
FAT_src[26] = $30+DATE_O
FAT_src[27] = ":"
FAT_src[28] = $30+YR_T
FAT_src[29] = $30+YR_O
FAT_src[30] = $d
FAT_src[31] = $a
FAT_src[32] = " "
FAT_src[33] = $30+T_HUNS
FAT_src[34] = $30+T_TENS
FAT_src[35] = $30+T_ONES
FAT_src[36] = " "
FAT_src[37] = "F"
FAT_src[38] = $d
FAT_src[39] = $a
FAT_src[40] = $d
FAT_src[41] = $a
FAT_count = 42
GOSUB FSfwrite


IF (FAT_error = 10) THEN STOP
IF (FAT_error != 0) THEN STOP


SDCLOSE:
' Close file
GOSUB FSfclose
IF (FAT_error != 0) THEN STOP
PAUSE 5000
CIU = 0
GOTO mainloop
I'm using Pic4550, 4Mhz crystal, PBP2.60. Can you tell me what I'm doing wrong in my setup?

thanks in advance,
tacbanon

tacbanon
- 23rd October 2011, 13:46
Okay I missed this line of code in the main loop "GOTO SD_WRITE".
The modified mainloop..
mainloop:
Gosub READ_RTC ' Read the time from the RTC
' Display time on LCD
Lcdout $fe, 128,"Date:", CRON_D[1],CRON_D[2],CRON_D[3],CRON_D[4],CRON_D[5],CRON_D[6],CRON_D[7],CRON_D[8]
Lcdout $fe, $c0, "Time:", CRON[1], CRON[2],CRON[3],CRON[4],CRON[5],CRON[6],CRON[7],CRON[8]
Pause 500
GOSUB USB_DISPLAY
IF (SD_WE = 0) AND (SD_CD = 0) THEN
GOTO SD_WRITE
ELSE
PWM LED,25,250
USBOUT 3, TEXT_NO_CARD, 10, mainloop
PAUSE 500
ENDIF

Goto mainloopBut this time even the rtc output is not displaying in the communication terminal and freezed on the lcd as well...I'm lost with the sdcard code part.

tacbanon

mackrackit
- 23rd October 2011, 18:21
'SD CARD ROUTINES
SD_WRITE:
CIU = 1
SDINIT:
' FSInit initializes the card and reads all the preliminary information from it
GOSUB FSInit
IF (FAT_error != 0) THEN STOP
Create a BLINK routine to blink an LED.
Change the above THEN STOP to THEN BLINK.
We need to see if you are getting an error. Better yet, send the error to a display to see what it is.

tacbanon
- 24th October 2011, 00:30
Hi added the following code to SDcard routines as suggested..

'SD CARD ROUTINES
SD_WRITE:
CIU = 1
SDINIT:
' FSInit initializes the card and reads all the preliminary information from it
GOSUB FSInit
IF (FAT_error != 0) THEN BLINK


' Display card directory
GOSUB FINDfirst ' Find first file on card
WHILE (FAT_error = 0)
GOSUB FINDnext ' Find next file on card
WEND


BLINK:
Toggle LED1
Lcdout $fe, 1
Lcdout $fe, 128,"I Blink"
return

My observation, when powered Rtc displays infos in the lcd after a couple second it goes to the BLINK routine and toggled the LED1 and displays "Blink". But after 6 seconds the lcd goes blank. BTW from the start of the program I can not connect to communication port its says "Serial port not available"
This my SDCARD Connection:
SCLK -> PortA.3
MOSI -> PortA.5
SD_CS -> PortC.2
MISO -> PortC.1
6084

regards,
tacbanon

mackrackit
- 24th October 2011, 19:27
Do you have a data sheet for the SD card module?

tacbanon
- 24th October 2011, 22:41
Hi, I don't have data sheet. I tried to search on www.lcsoft.net (http://www.lcsoft.net) but there was none. But I was able to test this module successfully on a separate program using the following code.


asm
__CONFIG _CONFIG1L, _PLLDIV_5_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
__CONFIG _CONFIG2L, _PWRT_ON_2L & _BOR_ON_2L & _BORV_2_2L & _VREGEN_ON_2L
__CONFIG _CONFIG2H, _WDT_OFF_2H
__CONFIG _CONFIG3H, _MCLRE_ON_3H & _LPT1OSC_OFF_3H & _PBADEN_OFF_3H & _CCP2MX_ON_3H
__CONFIG _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _ICPRT_OFF_4L & _XINST_OFF_4L & _DEBUG_OFF_4L
endasm
DEFINE OSC 48

' Alias PIC pins and registers for SD/MMC card
SD_WE Var PORTA.4 ' SD card write protect
SD_WE_TRIS Var TRISA.4 ' SD card write protect direction
SDI Var PORTB.0 ' SPI data in
SDI_TRIS Var TRISB.0 ' SPI data in direction
SCL Var PORTB.1 ' SPI clock
SCL_TRIS Var TRISB.1 ' SPI clock direction
SD_CS Var PORTB.3 ' SD card chip select
SD_CS_TRIS Var TRISB.3 ' SD card chip select direction
SD_CD Var PORTB.4 ' SD card detect
SD_CD_TRIS Var TRISB.4 ' SD card detect direction

SDO Var PORTC.7 ' SPI data out
SDO_TRIS Var TRISC.7 ' SPI data out direction

Include "SDFS.PBP"
SDC_UseHardSPI = TRUE ' Use hardware SSP port for SPI.

ADCON1 = 15 ' All I/O pins digital Pause 100

' FSInit initializes the card and reads all the preliminary information from it
Gosub FSInit
Serout2 PORTC.6, 84, ["Init: ", Dec FAT_error, " ", Dec SDC_status, " ", Dec SDC_response, $d, $a]
If (FAT_error != 0) Then Stop

' Display card directory
Gosub FINDfirst ' Find first file on card
While (FAT_error = 0)
Serout2 PORTC.6, 84, [Str FAT_FileName\11, $d, $a]
Gosub FINDnext ' Find next file on card
Wend

' This section defines a specific short (8.3) filename
' Note that spaces are use in empty elements and must be upper case for Windows
FAT_FileName[0] = "T"
FAT_FileName[1] = "E"
FAT_FileName[2] = "S"
FAT_FileName[3] = "T"
FAT_FileName[4] = "1"
FAT_FileName[5] = " "
FAT_FileName[6] = " "
FAT_FileName[7] = " "
FAT_FileName[8] = "T"
FAT_FileName[9] = "X"
FAT_FileName[10] = "T"

' Set file time to 8:30:10 and date to 1/1/2008
FAT_seconds = 5
FAT_minutes = 30
FAT_hours = 8
FAT_day = 1
FAT_month = 1
FAT_year = 28

' Open a file for write
FAT_mode = "w" ' Write mode
Gosub FSfopen ' Open file pointed to by Byte array FAT_FileName
Serout2 PORTC.6, 84, ["Open for write: ", Dec FAT_error, $d, $a]
If (FAT_error != 0) Then Stop

' Write to file
FAT_src[0] = "P"
FAT_src[1] = "r"
FAT_src[2] = "0"
FAT_src[3] = "t"
FAT_src[4] = "o"
FAT_src[5] = "t"
FAT_src[6] = "y"
FAT_src[7] = "p"
FAT_src[8] = "e"
FAT_count = 9
Gosub FSfwrite
Serout2 PORTC.6, 84, [ "Write ", Dec FAT_error, $d, $a]
If (FAT_error != 0) Then Stop

' Close file
Gosub FSfclose
Serout2 PORTC.6, 84, [ "Close ", Dec FAT_error, $d, $a]
If (FAT_error != 0) Then Stop

' Open a file for read
FAT_mode = "r" ' Read mode
Gosub FSfopen ' Open file pointed to by Byte array FAT_FileName
Serout2 PORTC.6, 84, ["Open: ", Dec FAT_error, $d, $a]
If (FAT_error != 0) Then Stop

' Read and display the whole file
FAT_count = 1 ' Read 1 byte to buffer at a time
Gosub FSfread
While (FAT_error = 0)
Serout2 PORTC.6, 84, [FAT_dest[0]]
FAT_count = 1 ' Read 1 byte to buffer at a time
Gosub FSfread
Wend
Serout2 PORTC.6, 84, [ "Read: ", Dec FAT_error, $d, $a]
End

regards,
tacbanon

tacbanon
- 28th October 2011, 02:40
Hi, I'm trying to test the RTC module on pic16F877A, according to the pins assignment SDA is on RC4 and SCL on RC3.


'RTC pins on 4550
SDA Var PORTB.0
SCL Var PORTB.1
'The above code works


'RTC pins on 877a
SDA Var PORTC.4
SCL Var PORTC.3

But this does not
What do you think I'm missing?
this is the code I'm using

DEFINE OSC 20CLEAR


;----[LCD definitions]------------------------------------------------------
DEFINE LCD_DREG PORTB
DEFINE LCD_DBIT 0
DEFINE LCD_EREG PORTB
DEFINE LCD_EBIT 5
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBIT 4
DEFINE LCD_BITS 4
DEFINE LCD_LINES 2
DEFINE LCD_COMMANDUS 2000
DEFINE LCD_DATAUS 50
'RTC pins on 877a
SDA Var PORTC.4
SCL Var PORTC.3


' Allocate variables
RTCYear Var Byte
RTCMonth Var Byte
RTCDate Var Byte
RTCDay Var Byte
RTCHour Var Byte
RTCMin Var Byte
RTCSec Var Byte
RTCCtrl Var Byte


TRISC= %11111111
;DB0 var byte[8]
CMCON = %00000111 ' Comparators = off


' Initialize LCD
LCDOUT $FE,1:FLAGS=0:PAUSE 250:LCDOUT $FE,1:PAUSE 250


' Set initial time
RTCYear = $10
RTCMonth = $03
RTCDate = $23
RTCDay = $02
RTCHour = $19
RTCMin = $35
RTCSec = 0
RTCCtrl = 0

'Gosub set ' Set the time


Goto mainloop ' Skip over subroutines


' Subroutine to write time to RTC
set:
I2CWrite SDA,SCL,$D0,$00,[RTCSec,RTCMin,RTCHour,RTCDay,RTCDate,RTCMonth,RTCY ear,RTCCtrl]
Return


' Subroutine to read time from RTC
gettime:
I2CRead SDA,SCL,$D0,$00,[RTCSec,RTCMin,RTCHour,RTCDay,RTCDate,RTCMonth,RTCY ear,RTCCtrl]
Return


mainloop:
Gosub gettime ' Read the time from the RTC


Lcdout $fe, 128,"Date:", hex2 RTCDate, "/",hex2 RTCMonth, "/" , hex2 RTCYear
Lcdout $fe, $c0, "Time:", hex2 RTCHour, ":", hex2 RTCMin, ":", hex2 RTCSec


Pause 500


Goto mainloop

regards,
tacbanon

mackrackit
- 28th October 2011, 15:46
My guess is to try it on pins that are not Schmitt triggered.

tacbanon
- 29th October 2011, 03:17
Hi mackrackit, I was able to run the RTC on PortB.0(SCL) and PortB.1(SDA). I'm still confused. I tried PortA ports that has TTL but did not work. and I have read other thread that they were able to run their program using Pic16F877A to read RTC module on PORTC.1 and PORTC.0 (Post# 1 http://www.picbasic.co.uk/forum/showthread.php?t=12671) can you help me point out how it works? I'm amazed that my lcd is also using PortB.0 and PortB.1 at the same time with the RTC required pins. BTW now I'm on Pic16F877A, does this mean that my sdcard module will not work on this chip?

thanks for the patients,
tacbanon

tacbanon
- 29th October 2011, 08:04
oops sorry for the typos(patients) what I meant is patience :)

mackrackit
- 30th October 2011, 08:49
Well, I am not sure why your code is not working on PORTC.3 and PORTC.4. I just tried it here and it is running. The code I used is below.

Port A. Did you turn the analog off? ADC

You have to use an 18Fxx with SDFS as LONG variables are needed.



' 16F877A RTC
' 10/30/2011
DEFINE OSC 4
#CONFIG
__config _HS_OSC & _WDT_ON & _LVP_OFF & _CP_OFF
#ENDCONFIG


LED VAR PORTB.5
TX VAR PORTD.1 ' DATA SEND PIN
BAUD CON 18030 ' 18030 = 600 BAUD


'RTC pins on 877a
SDA Var PORTC.4
SCL Var PORTC.3


' Allocate variables
RTCYear Var Byte
RTCMonth Var Byte
RTCDate Var Byte
RTCDay Var Byte
RTCHour Var Byte
RTCMin Var Byte
RTCSec Var Byte
RTCCtrl Var Byte


TRISC= %11111111
CMCON = %00000111


' Set initial time
RTCYear = $10
RTCMonth = $03
RTCDate = $23
RTCDay = $02
RTCHour = $19
RTCMin = $35
RTCSec = 0
RTCCtrl = 0

'Gosub set ' Set the time


Goto mainloop ' Skip over subroutines


' Subroutine to write time to RTC
set:
I2CWrite SDA,SCL,$D0,$00,[RTCSec,RTCMin,RTCHour,RTCDay,RTCDate,RTCMonth,RTCY ear,RTCCtrl]
Return


' Subroutine to read time from RTC
gettime:
I2CRead SDA,SCL,$D0,$00,[RTCSec,RTCMin,RTCHour,RTCDay,RTCDate,RTCMonth,RTCY ear,RTCCtrl]
Return


mainloop:
Gosub gettime ' Read the time from the RTC
TOGGLE LED
SEROUT2 TX, BAUD,["Date:", hex2 RTCDate, "/",hex2 RTCMonth, "/" , hex2 RTCYear,13]
SEROUT2 TX, BAUD,["Time:", hex2 RTCHour, ":", hex2 RTCMin, ":", hex2 RTCSec,13]


' Lcdout $fe, 128,"Date:", hex2 RTCDate, "/",hex2 RTCMonth, "/" , hex2 RTCYear
' Lcdout $fe, $c0, "Time:", hex2 RTCHour, ":", hex2 RTCMin, ":", hex2 RTCSec


Pause 500
GOTO mainloop

tacbanon
- 30th October 2011, 13:51
okay I think I know whats the problem, I found out that one my file I'm working has this code ADCON1 = %00001111 ' A/D converter off...which is different from the file I post. So this means I have to work on 18F.:)

thanks again,
tacbanon

tacbanon
- 30th October 2011, 16:54
Hi, in the PIC pins and registers for SD/MMC card

SD_WE Var PORTA.4 ' SD card write protect
SD_WE_TRIS Var TRISA.4 ' SD card write protect direction
SDI Var PORTB.0 ' SPI data in
SDI_TRIS Var TRISB.0 ' SPI data in direction
SCL Var PORTB.1 ' SPI clock
SCL_TRIS Var TRISB.1 ' SPI clock direction
SD_CS Var PORTB.3 ' SD card chip select
SD_CS_TRIS Var TRISB.3 ' SD card chip select direction
SD_CD Var PORTB.4 ' SD card detect
SD_CD_TRIS Var TRISB.4 ' SD card detect direction
SDO Var PORTC.7 ' SPI data out
SDO_TRIS Var TRISC.7 ' SPI data out direction
PortB.0. PortB.1,PortB.3 and PortB.4 is already in use by a keypad, can I move it to PortD instead?
Like this...


SD_WE Var PORTA.4 ' SD card write protect
SD_WE_TRIS Var TRISA.4 ' SD card write protect direction
SDI Var PORTD.0 ' SPI data in
SDI_TRIS Var TRISD.0 ' SPI data in direction
SCL Var PORTD.1 ' SPI clock
SCL_TRIS Var TRISD.1 ' SPI clock direction
SD_CS Var PORTD.3 ' SD card chip select
SD_CS_TRIS Var TRISD.3 ' SD card chip select direction
SD_CD Var PORTD.4 ' SD card detect
SD_CD_TRIS Var TRISD.4 ' SD card detect direction
SDO Var PORTC.7 ' SPI data out
SDO_TRIS Var TRISC.7 ' SPI data out direction

Regards,
tacbanon

mackrackit
- 30th October 2011, 17:06
That should not be a problem.

tacbanon
- 31st October 2011, 01:36
Hi, before I change to my desired pins, I made a test(using the original code)...but gives me this "Init: 6 1 255". I don't understand the code I used for testing works before.
Do you know what's causing it?

regards,
tacbanon

mackrackit
- 31st October 2011, 06:14
Open the SDFS file and look at the CE_xxx .
Your card did not initialize. Could be lots of things from a loose wire to a bad card. Bad as in file format corrupt to just wore out.
If the connections are good see if the card can be read in a PC, check the format.

tacbanon
- 31st October 2011, 08:18
Oh my, I think I broke my sdcard module. I double checked the wires I'm using and I reconnect and disconnect them to the devboard more than 10 times still the same result. The SDCard is okay (its in FAT format). I remembered I connect it to PortB earlier the same port where the keypad is also connected. :( this could have ruin the module...

tacbanon
- 1st November 2011, 01:17
For the mean time while I'm waiting for my new sdcard module(will come within a week). I will try to study on eeprom(Pic18F4550's internal). Thank you again to this wonderful forum(people) and to macrackit for the precious time... until nextime.

regards,
tacbanon

tacbanon
- 1st November 2011, 14:26
Hi, I've been playing some time with READ and Write command on the internal eeprom, I'm having trouble updating DATA @6,5,"54321". Probably I'm doing it the hard way. Can you help me out what I'm doing wrong?

'My Tested Prototype with Pic18F4550 and EEPROMInclude "modedefs.bas"
INCLUDE "C:\PBP\USB18\EE_Vars.pbp" ; Include the EE_var routines
asm
__CONFIG _CONFIG1L, _PLLDIV_5_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
__CONFIG _CONFIG2L, _PWRT_ON_2L & _BOR_ON_2L & _BORV_2_2L & _VREGEN_ON_2L
__CONFIG _CONFIG2H, _WDT_OFF_2H
__CONFIG _CONFIG3H, _MCLRE_ON_3H & _LPT1OSC_OFF_3H & _PBADEN_OFF_3H & _CCP2MX_ON_3H
__CONFIG _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _ICPRT_OFF_4L & _XINST_OFF_4L & _DEBUG_OFF_4L
endasm
DEFINE OSC 48
ADCON1 = 15 ' Set all I/Os to Digital
CMCON = 7 ' Disable Comparators
INTCON2.7 = 0
'EEprom data
'U1 VAR WORD : @ EE_var _U1, WORD, 9999 ' for later use

DATA @0,5,"12345"
DATA @6,5,"54321"
DATA @13,5,"14332"
DATA @19,5,"24432"
DATA @25,5,"85921"


TRISA = %00000000
TRISD = %00000000
PORTD = %00000000
TRISB = %11110000 ' Set Keypad I/O
PORTB = 0 ' Set columns LOW
PortA = 0


myvarkey var byte
ByteA var Byte[6]
B0 var byte[5]
cnt var byte
x var byte
cnt = 0
asciichar var byte
address var byte
' ---------------------------------[Program Start]----------------------------------------------
serout2 PortD.5,T9600,[$1B,$63,$30]
pause 200
serout2 PortD.5,84,[$1B,$45,"EEPROM"]
serout2 PortD.5,84,[$D,"Studies"]
serout2 PortD.5,84,[$1B,$63,$30]
pause 2000
Serout2 PortD.5,84, [$1B,$45]
main:


@ READKEYPAD _myvarkey
lookup myvarkey,[0,"123A456B789C*0#D"],Key
Serout2 PortD.5,84, [$1B,$45,"Key = ",Key]

if Key = "#" and cnt < 6 then
if ByteA[0]="1" and ByteA[1]="9" then ' wait 19 is pressed before updating
Serout2 PortD.5,84, [$D, "Special Key"]
pause 20
serout2 PortD.5,84,[$D,str ByteA\cnt]
READ 6,x
for address=6 to x+6
Write address+1, ByteA[address]
next address
cnt=0
endif

else
ByteA[cnt]=Key
Serout2 PortD.5,84, [$D, ">" , ByteA[cnt]]
cnt=cnt+1


Endif


if cnt > 5 then
Serout2 PortD.5,84, [$D, "5Digits only"]
cnt = 0
ENdif




if Key = "A" then
READ 0,x
serout2 PortD.5,84,[$D,Dec x," "]
pause 1000
for address=0 to x
READ address,asciichar
serout2 PortD.5,84,[$D,asciichar]
next address
pause 1000
cnt = 0
endif


if Key = "B" then
READ 6,x
serout2 PortD.5,84,[$D,Dec x," "]
pause 1000
for address=0+6 to x+6
READ address,asciichar
serout2 PortD.5,84,[$D,asciichar]
next address
pause 1000
cnt = 0
endif


if Key = "C" then
READ 13,x
serout2 PortD.5,84,[$D,Dec x," "]
pause 1000
for address=0+13 to x+13
READ address,asciichar
serout2 PortD.5,84,[$D,asciichar]
next address
pause 1000
cnt = 0
endif


if Key = "D" then
READ 19,x
serout2 PortD.5,84,[$D,Dec x," "]
pause 1000
for address=0+19 to x +19
READ address,asciichar
serout2 PortD.5,84,[$D,asciichar]
next address
pause 1000
cnt = 0
endif


if Key = "*" then
READ 25,x
serout2 PortD.5,84,[$D,Dec x," "]
pause 1000
for address=0+25 to x +25
READ address,asciichar
serout2 PortD.5,84,[$D,asciichar]
next address
pause 1000
cnt = 0
endif
pause 500
goto main
But updating Data @0 ,"12345" was not a problem, I'm stuck passed @0 location.

regards,
tacbanon

mackrackit
- 2nd November 2011, 01:19
This:

DATA @0,"12345"
DATA @6,"54321"

produces this when the EEPROM is read back


31
32
33
34
35
00
35
34
33
32
31
00
FF
FF



Is that what you want?

Did you find out what went bad with the SD card module? You can always use the SD card socket without all the other stuff on the module, it is easy if the MCU is running ~3 volts.

tacbanon
- 2nd November 2011, 11:27
DATA @0,"12345"
DATA @6,"54321"
Sorry If I was not being clear... how can I rewrite them and read back as in the format of "56988" or "65431".
Before overwriting DATA @6,"54321" I can read back as "54321". But after updating it gives me like Hex numbers.

I don't know exactly what happened to sdcard it just dont work as before. But I will try to test it again later.

thanks,
tacbanon

tacbanon
- 2nd November 2011, 11:56
Hi mackrackit, sorry if was not making clear. I want to read back from eeprom in the format of example "12345" or "65432" and I can do this uisng the following code.


if Key = "A" then
READ 0,x
serout2 PortD.5,84,[$D,Dec x," "]
pause 1000
for address=0 to x
READ address,asciichar
serout2 PortD.5,84,[$D,asciichar]
next address
pause 1000
cnt = 0
endif
And I noticed that if I rewrite the value of DATA @6,5,"54321" by entering "19325" and read it back I get "QZu" not "19325"..how do I resolve this?
I will try to test the sdcard tonight, hope something comes up....

thanks,
tacbanon

tacbanon
- 2nd November 2011, 13:57
Hi I finally got my eeporm problem solved..I figured that it needs 2 location space in between.


DATA @0,5,"12345"
DATA @7,5,"54321"
DATA @14,5,"14332"
DATA @21,5,"24432"
DATA @28,5,"85921"
works great...:)
BTW just want to ask if there is a possible to press a key that consist of several characters, for example if I pressed key 1 in successive I can get a character 'a','b','c'. similar to a cellphone keypad.

regards,
tacbanon

tacbanon
- 2nd November 2011, 14:14
I found one link http://www.picbasic.co.uk/forum/showthread.php?t=11209 but not sure how to to do it...but I will try..

regards,
tacbanon

mackrackit
- 2nd November 2011, 23:21
Melanie's example is good, but the methods talked about at the start of this thread might be better.
Timer/Counter from TIMER0.
It should work well with all of the other stuff going on.

tacbanon
- 3rd November 2011, 10:10
Hi, I'm sorry about that(I'm misleading the thread), anyway I borrowed an sdcard from a friend...but he never tested it yet and doesn't have datasheet..hopefully I don't blow it up :)
Here are the List I done & planning to do on my project in this thread.
1. Use Timer/Counter from TIMER0 for multi coin-acceptor(pic16F877 and 18F4550) -Done
2. Test RTC and Sdcard separate program- Done
3. Incorporating Multi coin acceptor-RTC and Keypad on 4550 - Done (I need the keypad for access and view settings.)
4. Incorporate SDcard for saving RTC data and number of coins dropped - not yet
5. Use USB CDC to dump data(rtc and number of coins) from SDcard to terminal communicator - not yet


I appreciated the help and the knowledge I got on this forum...not easy task(for me) but you encourage me to try it out...:)


thanks again,
tacbanon

tacbanon
- 3rd November 2011, 12:38
Hi, I have only one 4550 and I want to try it to another chip, I only have pic18f2550..I followed some instructions to comment the default config fuse in PBP INC files. I tried to compile this part of the code..


'<FL_PIC18F2550>' '<FL_PBPL>'
DEFINE OSC 48
@ __CONFIG _CONFIG1L, _PLLDIV_1_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
@ __CONFIG _CONFIG1H, _FOSC_HSPLL_HS_1H
@ __CONFIG _CONFIG2H, _WDT_OFF_2H & _WDTPS_512_2H
@ __CONFIG _CONFIG2L, _PWRT_ON_2L & _VREGEN_ON_2L
@ __CONFIG _CONFIG3H, _PBADEN_OFF_3H & _MCLRE_OFF_3H
@ __CONFIG _CONFIG4L, _LVP_OFF_4L & _XINST_OFF_4L
Include "modedefs.bas"
' Alias PIC pins and registers for SD/MMC card
SD_WE VAR PORTA.4 ' SD card write protect
SD_WE_TRIS VAR TRISA.4 ' SD card write protect direction
SDI VAR PORTA.5 ' SPI data in SD #7
SDI_TRIS VAR TRISA.5 ' SPI data in direction
SCL VAR PORTA.3 ' SPI clock SD #5
SCL_TRIS VAR TRISA.3 ' SPI clock direction
SD_CS VAR PORTC.2 ' SD card chip select SD #1
SD_CS_TRIS VAR TRISC.2 ' SD card chip select direction
SD_CD VAR PORTC.0 ' SD card detect
SD_CD_TRIS VAR TRISC.0 ' SD card detect direction
SDO VAR PORTC.1 ' SPI data out SD #2
SDO_TRIS VAR TRISC.1 ' SPI data out direction
But gives me the following compilation error...
6109

regards,
tacbanon

mackrackit
- 5th November 2011, 09:35
Use the same configs for the 2550 as you did for the 4550.

The only thing you need to do to move to the 2550 is change some pin assignments if you were using some on the 4550 that the 2550 does not have.

Make sure you have all of the needed files in the new project directory.

tacbanon
- 5th November 2011, 15:33
Thanks mackrackit, I just found out that the sdcard module I ordered will take a while(2weeks from china). The sdcard I borrowed did not work either.In the meantime...I will be working on the keypad codes for accessibility feature of this project...be back soon.

tacbanon
- 26th November 2011, 16:32
Hi, I got my new sdcard just the other day...now I've been trying to make following code work... I did tried to connect to another ports/pins but was not able to make it run.


Include "modedefs.bas"


asm
__CONFIG _CONFIG1L, _PLLDIV_5_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
__CONFIG _CONFIG2L, _PWRT_ON_2L & _BOR_ON_2L & _BORV_2_2L & _VREGEN_ON_2L
__CONFIG _CONFIG2H, _WDT_OFF_2H
__CONFIG _CONFIG3H, _MCLRE_ON_3H & _LPT1OSC_OFF_3H & _PBADEN_OFF_3H & _CCP2MX_ON_3H
__CONFIG _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _ICPRT_OFF_4L & _XINST_OFF_4L & _DEBUG_OFF_4L
endasm
DEFINE OSC 48


' Alias PIC pins and registers for SD/MMC card
SD_WE VAR PORTA.4 ' SD card write protect
SD_WE_TRIS VAR TRISA.4 ' SD card write protect direction
SDI VAR PORTA.5 ' SPI data in SD #7
SDI_TRIS VAR TRISA.5 ' SPI data in direction
SCL VAR PORTA.3 ' SPI clock SD #5
SCL_TRIS VAR TRISA.3 ' SPI clock direction
SD_CS VAR PORTC.2 ' SD card chip select SD #1
SD_CS_TRIS VAR TRISC.2 ' SD card chip select direction
SD_CD VAR PORTC.0 ' SD card detect
SD_CD_TRIS VAR TRISC.0 ' SD card detect direction
SDO VAR PORTC.1 ' SPI data out SD #2
SDO_TRIS VAR TRISC.1 ' SPI data out direction


' Include the SD/MMC subroutines (found in http://melabs.com/resources/samples/pbp/sdfs3.zip)
cnt var word
cnt = 0
Include "SDFS.PBP"
SDC_UseHardSPI = TRUE ' Use hardware SSP port for SPI.




ADCON1 = 15 ' All I/O pins digital
Pause 100


' FSInit initializes the card and reads all the preliminary information from it
Gosub FSInit
Serout2 PORTC.6, 84, ["Init: ", Dec FAT_error, " ", Dec SDC_status, " ", Dec SDC_response, $d, $a]
If (FAT_error != 0) Then Stop


' This section defines a specific short (8.3) filename
' Note that spaces are use in empty elements and must be upper case for Windows
FAT_FileName[0] = "R"
FAT_FileName[1] = "E"
FAT_FileName[2] = "C"
FAT_FileName[3] = "O"
FAT_FileName[4] = "R"
FAT_FileName[5] = "D"
FAT_FileName[6] = "S"
FAT_FileName[7] = " "
FAT_FileName[8] = "T"
FAT_FileName[9] = "X"
FAT_FileName[10] = "T"


FAT_seconds = 5
FAT_minutes = 30
FAT_hours = 8
FAT_day = 1
FAT_month = 1
FAT_year = 28




' Open a file for write
FAT_mode = "A" ' Write mode
Gosub FSfopen ' Open file pointed to by Byte array FAT_FileName
Serout2 PORTC.6, 84, ["Open for write: ", Dec FAT_error, $d, $a]
If (FAT_error != 0) Then Stop


cnt = cnt + 5
' Write to file
FAT_src[0] = $30+cnt
FAT_src[1] = "M"
FAT_src[2] = "I"
FAT_src[3] = "C"
FAT_src[4] = "R"
FAT_src[5] = "O"
FAT_src[6] = "T"
FAT_src[7] = "E"
FAT_src[8] = "S"
FAT_src[9] = "T"
FAT_src[10] = 10

FAT_count = 11
Gosub FSfwrite
Serout2 PORTC.6, 84, [ "Write ", Dec FAT_error, $d, $a]
If (FAT_error != 0) Then Stop




' Close file
Gosub FSfclose
Serout2 PORTC.6, 84, [ "Close ", Dec FAT_error, $d, $a]
End

Haredware: Pic16F4550, PBP2.60, crystal 20Mhz
The alias PIC pins and registers for SDcard is a copy from Macrackit, Can you tell me what I'm doing wrong?
The alias pins below works with no problem..


' Alias PIC pins and registers for SD/MMC card
SD_WE Var PORTA.4 ' SD card write protect
SD_WE_TRIS Var TRISA.4 ' SD card write protect direction
SDI Var PORTB.0 ' SPI data in
SDI_TRIS Var TRISB.0 ' SPI data in direction
SCL Var PORTB.1 ' SPI clock
SCL_TRIS Var TRISB.1 ' SPI clock direction
SD_CS Var PORTB.3 ' SD card chip select
SD_CS_TRIS Var TRISB.3 ' SD card chip select direction
SD_CD Var PORTB.4 ' SD card detect
SD_CD_TRIS Var TRISB.4 ' SD card detect direction
SDO Var PORTC.7 ' SPI data out
SDO_TRIS Var TRISC.7 ' SPI data out direction

But I want to move the pins off from PortB.

Thanks and regards,
tacbanon

mackrackit
- 26th November 2011, 23:04
What errors are you getting?

Serout2 PORTC.6, 84, ["Init: ", Dec FAT_error, " ", Dec SDC_status, " ", Dec SDC_response, $d, $a]

tacbanon
- 26th November 2011, 23:59
Hi macrackit, I do not have any errors, I've been playing with it the whole night last night but no progress...
I also tried the following alias pins configurations from a book..(uising pic18F452) but no luck.


' Alias PIC pins and registers for SD/MMC card
SD_WE VAR PORTA.4 ' SD card write protect
SD_WE_TRIS VAR TRISA.4 ' SD card write protect direction
SDI VAR PORTC.5 ' SPI data in SD #7
SDI_TRIS VAR TRISC.5 ' SPI data in direction
SCL VAR PORTC.3 ' SPI clock SD #5
SCL_TRIS VAR TRISC.3 ' SPI clock direction
SD_CS VAR PORTC.2 ' SD card chip select SD #1
SD_CS_TRIS VAR TRISC.2 ' SD card chip select direction
SD_CD VAR PORTC.0 ' SD card detect
SD_CD_TRIS VAR TRISC.0 ' SD card detect direction
SDO VAR PORTC.4 ' SPI data out SD #2
SDO_TRIS VAR TRISC.4 ' SPI data out direction



Hmm..I don't understand, CS -> RB3, MOSI -> RC7, SCLK -> RB1 and MISO -> RB0 works fine without a problem, but assigning them to another port/pins giving me trouble.:)
(BTW I'm using Pic18F4550, crystal 20Mhz)

regards,
tacbanon

tacbanon
- 27th November 2011, 00:54
@mackrackit

Serout2 PORTC.6, 84, ["Init: ", Dec FAT_error, " ", Dec SDC_status, " ", Dec SDC_response, $d, $a]
Hi, Sorry I spoke too soon, this is what I get when I'm trying to transfer to another port/pins "’nit: 6 2 0"

mackrackit
- 27th November 2011, 01:43
From the SDFS file, the 6 and 2 from the generated errors.

CE_INIT_ERROR Con 6 ' An initialization error has occured
sdcCardNotInitFailure Con 2


From section 10.3 18F4550 data sheet.

On a Power-on Reset, these pins, except
RC4 and RC5, are configured as digital
inputs. To use pins RC4 and RC5 as digi-
tal inputs, the USB module must be dis-
abled (UCON<3> = 0) and the on-chip
USB transceiver must be disabled
(UCFG<3> = 1).

And do you have the Capture / Compare turned off for PORTC?

tacbanon
- 27th November 2011, 04:10
Thanks for helping me out mackrackit..but I'm still having trouble running the following(I'm really sure if I'm getting the settings correctly).




Include "modedefs.bas"


asm
__CONFIG _CONFIG1L, _PLLDIV_5_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
__CONFIG _CONFIG2L, _PWRT_ON_2L & _BOR_ON_2L & _BORV_2_2L & _VREGEN_ON_2L
__CONFIG _CONFIG2H, _WDT_OFF_2H
__CONFIG _CONFIG3H, _MCLRE_ON_3H & _LPT1OSC_OFF_3H & _PBADEN_OFF_3H & _CCP2MX_ON_3H
__CONFIG _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _ICPRT_OFF_4L & _XINST_OFF_4L & _DEBUG_OFF_4L
endasm
DEFINE OSC 48


' Alias PIC pins and registers for SD/MMC card
SD_WE VAR PORTA.4 ' SD card write protect
SD_WE_TRIS VAR TRISA.4 ' SD card write protect direction
SDI VAR PORTC.5 ' SPI data in SD #7
SDI_TRIS VAR TRISC.5 ' SPI data in direction
SCL VAR PORTC.3 ' SPI clock SD #5
SCL_TRIS VAR TRISC.3 ' SPI clock direction
SD_CS VAR PORTC.2 ' SD card chip select SD #1
SD_CS_TRIS VAR TRISC.2 ' SD card chip select direction
SD_CD VAR PORTC.0 ' SD card detect
SD_CD_TRIS VAR TRISC.0 ' SD card detect direction
SDO VAR PORTC.4 ' SPI data out SD #2
SDO_TRIS VAR TRISC.4 ' SPI data out direction


' Include the SD/MMC subroutines (found in http://melabs.com/resources/samples/pbp/sdfs3.zip)
cnt var word
cnt = 0
Include "SDFS.PBP"

SDC_UseHardSPI = TRUE ' Use hardware SSP port for SPI.
ADCON1 = 15 ' All I/O pins digital
'--Added Pic settings
UCON.3 = 0 'USB module disabled
UCFG.3 = 1 'USB transceiver disabled
CCP1CON=0 ' Capture/Compare OFF
CCP2CON=0 '


Pause 100


' FSInit initializes the card and reads all the preliminary information from it
Gosub FSInit
Serout2 PORTC.6, 84, ["Init: ", Dec FAT_error, " ", Dec SDC_status, " ", Dec SDC_response, $d, $a]
If (FAT_error != 0) Then Stop


' This section defines a specific short (8.3) filename
' Note that spaces are use in empty elements and must be upper case for Windows
FAT_FileName[0] = "R"
FAT_FileName[1] = "E"
FAT_FileName[2] = "C"
FAT_FileName[3] = "O"
FAT_FileName[4] = "R"
FAT_FileName[5] = "D"
FAT_FileName[6] = "S"
FAT_FileName[7] = " "
FAT_FileName[8] = "T"
FAT_FileName[9] = "X"
FAT_FileName[10] = "T"


' Set file time to 8:30:10 and date to 1/1/2008
FAT_seconds = 5
FAT_minutes = 30
FAT_hours = 8
FAT_day = 1
FAT_month = 1
FAT_year = 28




' Open a file for write
FAT_mode = "A" ' Write mode
Gosub FSfopen ' Open file pointed to by Byte array FAT_FileName
Serout2 PORTC.6, 84, ["Open for write: ", Dec FAT_error, $d, $a]
If (FAT_error != 0) Then Stop


cnt = cnt + 5
' Write to file
FAT_src[0] = $30+cnt
FAT_src[1] = "M"
FAT_src[2] = "I"
FAT_src[3] = "C"
FAT_src[4] = "R"
FAT_src[5] = "O"
FAT_src[6] = "B"
FAT_src[7] = "O"
FAT_src[8] = "X"
FAT_src[9] = 13
FAT_src[10] = 10

FAT_count = 11
Gosub FSfwrite
Serout2 PORTC.6, 84, [ "Write ", Dec FAT_error, $d, $a]
If (FAT_error != 0) Then Stop




' Close file
Gosub FSfclose
Serout2 PORTC.6, 84, [ "Close ", Dec FAT_error, $d, $a]
End


Kindly check my hardware Connections
SDCARD MCU(4550)
CS ------ RC2
MOSI ------- RC5
SCK ------- RC3
MISO -------RC4
I'm not having a display in the terminal to identify the error produced, What do you think I'm missing?

Thanks in advance,
tacbanon

mackrackit
- 27th November 2011, 11:08
SDC_UseHardSPI = TRUE ' Use hardware SSP port for SPI.

Should be FALSE

tacbanon
- 27th November 2011, 14:29
That was it Dave, thank you for sharing your time...:)But I have another question, can I still make USB CDC with the setup we modified?regards,tacbanon

mackrackit
- 27th November 2011, 15:33
RC4 and RC5 are the USB pins.