Please write down exactly how did you enable the AD PIN! In which file what I have to modificate.
I writed a lot of C code to PIC, but this MPLAB C30 programming absolutely new to me. Is there any basic introduction? How to use ADC, PWM, timers, etc? I used CCS and its help were really useful.
Thank you!
Hi Alex,
I split this to a new topic so it gets more attention.
The intro tutorial describes how to use peripheral pin select, something you will need to assign pins to the PWM, timer, etc. It also describes UART and other simple peripheral setup:
http://dangerousprototypes.com/docs/Int ... rogramming (http://dangerousprototypes.com/docs/Introduction_to_dsPIC33_programming)
ADC, PWM, and timers are about the same as the UART once you're familiar with the datasheets. I can help with any specific questions and examples you need.
I read this intro tutorial, but that isn't a big help to me. I don't know how to use ADC by this PIC.
I found examples only for dsPIC30-s, is it same for dsPIC33-s?
I need few examples to learn use this compiler. (for this PIC! on webplatform)
I've successfully enabled the AD converter.
Help was the original Microchip MDD Demo App.
Hi Alex,
Thanks for the update, I'm glad you got it going.
Would you please share an example? I'll start a wiki page with simple peripheral demos.
It works in SD Card webserver. C30: c3.30b, TCPIP Stack: 5.36
Add to static void InitHardware(void) :
// ADC
AD1CHS0 = 0; // Input to AN0 (potentiometer)
AD1CON1 = 0x84E4; // Turn on, auto sample start, auto-convert, 12 bit mode (on parts with a 12bit A/D)
AD1CON2 = 0x0404; // AVdd, AVss, int every 2 conversions, MUXA only, scan
AD1CON3 = 0x1003; // 16 Tad auto-sample, Tad = 3*Tcy
Now I see on the homepage only numbers between 20 and 40, and it isn't stabil...
I cannot change the value with the potentiometer.
Any suggestion?
Why are you scanning? You just need to sample one pin. Try changing AD1CON2 to 0x0004
Doesn't work.
Now Pot: 0 and it isn't change. (the voltage on AN0 has no effect)
OK, I realized another error: you are turning ADC on and changing settings, try setting up AD1CON1 last.
No effect :(
With: AD1CON2 = 0x0404; // AVdd, AVss, int every 2 conversions, MUXA only, scan
it measures something, just don't know what
OK, on page 253 of the datasheet there is the ADC section (mine might be a little old). You are probably using the code written for PIC24F, they are more or less the same but there are differences in peripherals most of the time. So for this dsPIC initialization steps are:
1. Configure the ADC module:
a) Select port pins as analog inputs
(AD1PCFGH<15:0> or AD1PCFGL<15:0>)
b) Select voltage reference source to match
expected range on analog inputs
(AD1CON2<15:13>)
c) Select the analog conversion clock to
match desired data rate with processor
clock (AD1CON3<7:0>)
d) Determine how many S/H channels are
used (AD1CON2<9:8> and
AD1PCFGH<15:0> or AD1PCFGL<15:0>)
e) Select the appropriate sample/conversion
sequence (AD1CON1<7:5> and
AD1CON3<12:8>)
f) Select how conversion results are
presented in the buffer (AD1CON1<9:8>)
g) Turn on ADC module (AD1CON1<15>)
2. Configure ADC interrupt (if required):
a) Clear the AD1IF bit
b) Select ADC interrupt priority
So according to this:
AD1PCFGL = 0xFFFE; // AN0 selected, other pins digital
AD1CSSL = 0x0001; // Scan only AN0 (just in case)
AD1CHS0 = 0x0000; // Channel 0 negative is AVss, positive AN0 for Sample A and B
AD1CHS123 = 0x0000; // For other channels (We'll use 12Bit so CH 1,2,3 are not used, just in case)
AD1CON4 = 0x0000; // Allocate 1 word of buffer to each analog input
AD1CON3 = 0x1003; // Clock source system, 16 Tad auto-sample, Tad = 3*Tcy
AD1CON2 = 0x0000; // AVdd, AVss, no scan, CH0 only, increment DMA every sample, buffer filled at 0x0 always, Sample A
AD1CON1 = 0x84E4; // ADC on, cnt on Idle, DMA ordered, 12bit integer, auto-convert, auto-start
I just have the old one at hand, so I cannot try out the code. Let's see what this does.
Also try using ADC without web server code to learn how it works. Try to push the result to serial output (in ASCII format if possible). I'm up for writing some demos but I have the old version. I'm planning to get the new one soon, I will be visiting family next month.
Excellent!
It's working!
http://http://alex077.dyndns-at-work.com/
Thank you very much!
[quote author="alex077"]Excellent!
It's working!
http://http://alex077.dyndns-at-work.com/
Thank you very much![/quote]
I'm glad that worked! Congrats! :)
I have a new trouble :(
I want to use the internal RTCC. I copied the codes for RTCC from FREERTOS project. (rtcc.c and rtcc.h)
It seems correct working, but I don't know how to use exactly.
My InitHardware function (still in SD card webserver project):
rtcc_init(); at the end
in main:
...#include "RTCC.h"...
... char str[25]; //temporary storage for uitoa
...
AppConfig.SecondaryDNSServer.Val = MY_DEFAULT_SECONDARY_DNS_BYTE1 | MY_DEFAULT_SECONDARY_DNS_BYTE2<<8ul | MY_DEFAULT_SECONDARY_DNS_BYTE3<<16ul | MY_DEFAULT_SECONDARY_DNS_BYTE4<<24ul;
struct tm ts;
struct rtcc_bcd_tm t;
struct tm tt;
ts.tm_hour=24;
ts.tm_mday=26;
ts.tm_min=59;
ts.tm_mon=8;
ts.tm_sec=30;
ts.tm_wday=5;
ts.tm_year=2011;
rtcc_set_tm(ts);
StackInit(); //setup the stack
while(1){//never ending loop
if(FSready==FALSE) FSready=FileSystemInit(); //attempt to initialize the FAT SD card library
if((TickGet() - ticks) >= (TICK_SECOND/2)){ //blink LED
ticks = TickGet();
LED0_IO ^= 1;
if(BUTTON2_IO==0)
{
LED3_IO^=1;
putsUART3("nr");putsUART3("nr");
rtcc_get_tm(&tt);uitoa(tt.tm_hour,str);
putsUART3(str);putsUART3(":");
uitoa(tt.tm_min,str);//putsUART3(":");
putsUART3(str);putsUART3(":");
uitoa(tt.tm_sec,str);putsUART3(str);putsUART3("nr");
uitoa(tt.tm_year,str);putsUART3(str);putsUART3(".");
uitoa(tt.tm_mon,str);putsUART3(str);putsUART3(".");
uitoa(tt.tm_mday,str);putsUART3(str);putsUART3(".");
}
}
...
I have attached the source files of RTCC.
In my serial terminal now I see:
24:59:58
219.7.26.
24:59:59
219.7.26.
24:59:59
219.7.26.
25:0:0
219.7.26.
25:0:0
219.7.26.
25:0:1
219.7.26.
219 should be 2011
ts.tm_year=11; wasn't good (maybe it was 217..)
So it is a problem with the formatting. Any idea what I am doing wrong?
That wasn't good:
//putsUART3(tt.tm_hour);putsUART3(":");putsUART3(tt.tm_min);
//putsUART3(":");putsUART3(tt.tm_sec);I think by setting is the format wrong.
Update:
My settings were wrong. My new (test) settings:
ts.tm_hour=23;
ts.tm_mday=31;
ts.tm_min=59;
ts.tm_mon=12;
ts.tm_sec=55;
ts.tm_wday=3;
ts.tm_year=11;
REsult:
...
23:59:59
267.11.31.
0:0:0
268.0.1.
...
Update2:
I made the following changes:
add: long yearstr[10];
ultoa(tt.tm_year,yearstr);putsUART3(yearstr);
Not uitoa!
But the result is still the same. How can I fix this year displaying problem?
With ts.tm_year=2011; the result is 220.0.1.
One of the Microchip examples were using RTC, the MDD data logger project. It is writing data in a strange way, have to check it more.
Aha, I guess I've found the error. Try using this function after you get as tm
void rtcc_tm_to_vals( const struct tm time, short *year, short *month_date, short *wday_hour, short *min_sec )
I guess I've found some parts in the code, the year is not set as the RTCC requests it. I'll do some changes to the code and upload them here in a few minutes.
OK, my changes are in RTCC.c file only. I've made some minor changes to these functions only:
void rtcc_get_bcd_tm(struct rtcc_bcd_tm *tptr)
void rtcc_get_tm(struct tm *tptr)
void rtcc_tm_to_vals( const struct tm time, short *year, short *month_date, short *wday_hour, short *min_sec )
Now you should be able to use year. It is last 2 digits only, starting from 2000, so ts.tm_year = 11 is used. Can you please try out rtcc_get_bcd_tm also? It should give you binary coded decimal values for date and whatnot.
PS: No need to use tm_to_vals after you use the get function. That is used during setting RTCC, there was a minor mistake (I guess) there too, so it is included in the list. Just get the struct and display its contents after changing them to ASCII.
Thank you very much for your help!
Here is my test code:
putsUART3("nr");putsUART3("nr");
rtcc_get_tm(&tt);
uitoa(tt.tm_hour,str); putsUART3(str); putsUART3(":");
uitoa(tt.tm_min,str); putsUART3(str); putsUART3(":");
uitoa(tt.tm_sec,str); putsUART3(str); putsUART3("nr");
ultoa(tt.tm_year+2000,yearstr); putsUART3(yearstr); putsUART3(".");
uitoa(tt.tm_mon+1,str); putsUART3(str); putsUART3(".");
uitoa(tt.tm_mday,str); putsUART3(str); putsUART3(".");putsUART3("nr");
putsUART3("BCD:t");
rtcc_get_bcd_tm(&t);
uitoa(bcdL2char(t.hour),str); putsUART3(str); putsUART3(":");
uitoa(bcdL2char(t.min),str); putsUART3(str); putsUART3(":");
uitoa(bcdL2char(t.sec),str); putsUART3(str); putsUART3("nrt20");
uitoa(bcdL2char(t.year),str); putsUART3(str); putsUART3(".");
uitoa(bcdL2char(t.mon),str); putsUART3(str); putsUART3(".");
uitoa(bcdL2char(t.day),str); putsUART3(str); putsUART3(".");Result:
23:59:58
2011.12.31.
BCD: 23:59:58
2011.12.31.
23:59:59
2011.12.31.
BCD: 23:59:59
2011.12.31.
0:0:0
2012.1.1.
BCD: 0:0:0
2012.1.1.
0:0:1
2012.1.1.
BCD: 0:0:1
2012.1.1.
0:0:2
2012.1.1.
BCD: 0:0:2
2012.1.1.
OK, I guess both tm and bcd work well.