Dangerous Prototypes

Other projects => Past projects => Web platform => Topic started by: alex077 on August 17, 2011, 08:22:19 pm

Title: Help with ADC, PWM, Timer, etc
Post by: alex077 on August 17, 2011, 08:22:19 pm
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!
Title: Re: Help with ADC, PWM, Timer, etc
Post by: ian on August 22, 2011, 10:20:35 am
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.
Title: Re: Help with ADC, PWM, Timer, etc
Post by: alex077 on August 23, 2011, 07:39:53 pm
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)
Title: Re: Help with ADC, PWM, Timer, etc
Post by: alex077 on August 23, 2011, 10:40:15 pm
I've successfully enabled the AD converter.
Help was the original Microchip MDD Demo App.
Title: Re: Help with ADC, PWM, Timer, etc
Post by: ian on August 24, 2011, 09:14:30 am
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.
Title: Re: Help with ADC, PWM, Timer, etc
Post by: alex077 on August 24, 2011, 09:32:51 pm
It works in SD Card webserver. C30: c3.30b, TCPIP Stack: 5.36
Add to static void InitHardware(void) :
Code: [Select]
// 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?
Title: Re: Help with ADC, PWM, Timer, etc
Post by: tayken on August 25, 2011, 05:26:26 am
Why are you scanning? You just need to sample one pin. Try changing AD1CON2 to 0x0004
Title: Re: Help with ADC, PWM, Timer, etc
Post by: alex077 on August 25, 2011, 05:13:04 pm
Doesn't work.
Now Pot: 0 and it isn't change. (the voltage on AN0 has no effect)
Title: Re: Help with ADC, PWM, Timer, etc
Post by: tayken on August 25, 2011, 06:09:28 pm
OK, I realized another error: you are turning ADC on and changing settings, try setting up AD1CON1 last.
Title: Re: Help with ADC, PWM, Timer, etc
Post by: alex077 on August 25, 2011, 08:35:34 pm
No effect :(

With:    AD1CON2 = 0x0404;         // AVdd, AVss, int every 2 conversions, MUXA only, scan
it measures something, just don't know what
Title: Re: Help with ADC, PWM, Timer, etc
Post by: tayken on August 25, 2011, 09:59:13 pm
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:

Code: [Select]
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:
Code: [Select]
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.
Title: Re: Help with ADC, PWM, Timer, etc
Post by: tayken on August 25, 2011, 10:07:05 pm
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.
Title: Re: Help with ADC, PWM, Timer, etc
Post by: alex077 on August 25, 2011, 10:30:01 pm
Excellent!
It's working!
http://http://alex077.dyndns-at-work.com/
Thank you very much!
Title: Re: Help with ADC, PWM, Timer, etc
Post by: tayken on August 25, 2011, 11:32:51 pm
[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! :)
Title: Re: Help with ADC, PWM, Timer, etc
Post by: alex077 on August 26, 2011, 09:47:41 pm
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:
Code: [Select]
...#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:
Code: [Select]
                                                                                                                                                                   
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:
Code: [Select]
//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:
Code: [Select]
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:
Code: [Select]
...
                                                                                                                                                                   
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.
Title: Re: Help with ADC, PWM, Timer, etc
Post by: tayken on August 27, 2011, 04:40:58 pm
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
Code: [Select]
void rtcc_tm_to_vals( const struct tm time, short *year, short *month_date, short *wday_hour, short *min_sec )
Title: Re: Help with ADC, PWM, Timer, etc
Post by: tayken on August 27, 2011, 05:12:03 pm
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.
Title: Re: Help with ADC, PWM, Timer, etc
Post by: tayken on August 27, 2011, 05:20:02 pm
OK, my changes are in RTCC.c file only. I've made some minor changes to these functions only:
Code: [Select]
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.
Title: Re: Help with ADC, PWM, Timer, etc
Post by: alex077 on August 28, 2011, 12:13:11 am
Thank you very much for your help!
Here is my test code:
Code: [Select]
               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:
Code: [Select]
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.
Title: Re: Help with ADC, PWM, Timer, etc
Post by: tayken on August 28, 2011, 07:41:35 am
OK, I guess both tm and bcd work well.

( ! ) Fatal error: Uncaught exception 'Elk_Exception' with message 'Please try again. If you come back to this error screen, report the error to an administrator.' in /var/www/dangerousprototypes/forum/sources/database/Db-mysql.class.php on line 696
( ! ) Elk_Exception: Please try again. If you come back to this error screen, report the error to an administrator. in /var/www/dangerousprototypes/forum/sources/database/Db-mysql.class.php on line 696
Call Stack
#TimeMemoryFunctionLocation
10.01292176568session_write_close ( )...(null):0
20.01322308160ElkArte\sources\subs\SessionHandler\DatabaseHandler->write( )...(null):0
30.01322308936Database_MySQL->query( ).../DatabaseHandler.php:119
40.05772447672Database_MySQL->error( ).../Db-mysql.class.php:273