Pages: [1]   Go Down
Author Topic: Millis and DAC square wave as monitored by TI-Vernier.. interesting bug  (Read 766 times)
0 Members and 1 Guest are viewing this topic.
Nyack, NY
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,
I've recently begun programming on the Due, and have been monitoring my pin outputs using a voltage probe and the data logging software on a TI nspire CX CAS calculator (see the screenshot attached).

After playing with the PWM pins, I attempted to make a square wave using pin DAC0. Here is the code:

Code:
void setup() {
  // put your setup code here, to run once:

}
int millisCurrent = 0;
int millisPrevious = 0;
int analogSetting1 = 800;
int analogSetting2 = 400;

void loop() {
  millisCurrent = millis();
 
if (millisCurrent-millisPrevious <200)
{analogWrite(DAC0,analogSetting1);}
else
{analogWrite(DAC0,analogSetting2);}

if (millisCurrent-millisPrevious >600)
{millisPrevious = millis();}
}

The idea is to have two different levels of analog output power stored, and to switch between them as the timer carries on. The first setting should be active for the first 200ms of a cycle, then the second setting until 600ms, when the cycle resets. Therefore the duration of analogSetting2 should be two times that of analogSetting1, right? I'm reading the opposite duration/amplitude relationship on my TI (that's the screenshot). Or have I just been staring at this for too long?


* Millis Square Wave.jpg (196.2 KB, 1280x960 - viewed 27 times.)
Logged

Offline Offline
God Member
*****
Karma: 32
Posts: 507
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You may need to use analogWriteResolution - I don't know what the default resolution is but if it's 8 bits then 800 mod 256 = 32 which is less than 400 mod 256 = 144, which would explain why 800 gives you a lower output voltage than 400.
Logged


Nyack, NY
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That's it, well at least I got my newbie post out of the way.
I was using parameter range 0-1023, which applies to analogRead. 0-255 is the range for analogWrite.
Logged

Pages: [1]   Go Up
Jump to: