Jump to content
43oh

Problem using Arduino code in Energia


Recommended Posts

I do use Serial.begin(9600)  :smile:

 

P.S I read this thread and decided to check if it's working on mine board. I connected P1.5 pin to GND and used this code. Still can't receive anything in Serial Monitor. I tried both M430G2553 & M430G2452. What's the problem  :x

static uint8_t sensorPin = A5;
int sensorValue = 0;

void setup() {
  Serial.begin(9600);
}
void loop() {
  sensorValue = analogRead(sensorPin);
  Serial.print(sensorValue);
  delay(10);
}
Link to post
Share on other sites
  • Replies 51
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Popular Posts

What about my my code? It just works I don't think you really need the sampling intervals to be spot on accuracy that you need a timer. If you really want it to be better just do   // Very simple

Yes and remove all the timer stuff from setup So something like this   // Very simple Arduino Lithium-ion battery capacity tester // from electronicsblog.net   #define LED 13 #define resistor 6.9

Your code is ATmega specific (you're directly addressing the timer peripheral of the Arduino), and hence cannot be run at the MSP430/launchpad. You could translate this code to work for the Launchpad.

Sorry, my fault. I was actually trying with both of them.

The problem was in Serial.print().

I used Serial.println() instead.

 

Here is the working source.

static uint8_t sensorPin = A5;
int sensorValue = 0;

void setup() {
  Serial.begin(9600);
}
void loop() {
  sensorValue = analogRead(sensorPin);
  Serial.println(sensorValue);
  delay(1000);
}

 

But I still can't get this battery test working.

Here is the code you gave me, is there anything wrong with it?

// Very simple Arduino Lithium-ion battery capacity tester
// from electronicsblog.net

static uint8_t sensorPin = A5;
#define LED GREEN_LED
#define resistor 13
 
float capacity=0, value,voltage,current, time=0;
 
void measure (void) {
 
  value= analogRead(sensorPin);
 
  voltage=value/1024*5.0;
 
  current = voltage/resistor;
 
  capacity=capacity+current/3600;
 
  time++;
 
Serial.print("Voltage= ");
Serial.print(voltage);

Serial.print("V Current= ");
Serial.print(current);

Serial.print("A Capacity= ");
Serial.print(capacity);
Serial.print("Ah ");

Serial.print("Discharging time= ");
Serial.print(time);
Serial.print("s ");

Serial.print("\n");
}
 
boolean x=false;
  
#pragma vector=TIMER0_A0_VECTOR
__interrupt void TIMER1_OVF_vect(void) {
    x=!x;
    digitalWrite(LED, x);
    if(x)
    {
        measure();
    }
    }

    void setup() {
    pinMode(LED, OUTPUT);
    // I don't know the ATmega timers, but I will be assuming your timer clock is running at 1MHz and interrupts every 0x10000-0x0BDC = 62500 microseconds (every 62.5 milliseconds)
    _BIS_SR(GIE); // enabled global interrupt;
    TACTL  = TAIE; // enabled timer overflow interrupt;
    TACTL  |= MC_1; // normal operation page 366 (mode up);
    TACCR0 = 62500 - 1; // set overflow value to remove time error (16bit counter register)
    TACTL  |= TASSEL_2 + TACLR; // start timer/ reset clock
    Serial.begin(96000);
     
    };
 
void loop () {
  LPM3;
};
Link to post
Share on other sites

I added this line of comment

// I don't know the ATmega timers, but I will be assuming your timer clock is running at 1MHz and interrupts every 0x10000-0x0BDC = 62500 microseconds (every 62.5 milliseconds)

So the timer will be executed every 62.5 ms, not once per second as you intended, use the second piece of code I pasted.

Link to post
Share on other sites

Ok, didn't noticed that part of you message.

The code looks like this now:

// Very simple Arduino Lithium-ion battery capacity tester
// from electronicsblog.net

static uint8_t sensorPin = A5;
#define LED GREEN_LED
#define resistor 13
 
float capacity=0, value,voltage,current, time=0;
 
void measure (void) {
 
  value= analogRead(sensorPin);
 
  voltage=value/1024*5.0;
 
  current = voltage/resistor;
 
  capacity=capacity+current/3600;
 
  time++;
 
Serial.print("Voltage= ");
Serial.print(voltage);

Serial.print("V Current= ");
Serial.print(current);

Serial.print("A Capacity= ");
Serial.print(capacity);
Serial.print("Ah ");

Serial.print("Discharging time= ");
Serial.print(time);
Serial.print("s ");

Serial.print("\n");
}
 
boolean x=false;
  
 #pragma vector=TIMER0_A0_VECTOR
__interrupt void TIMER1_OVF_vect(void) {
    x=!x;
    digitalWrite(LED, x);
    if(x)
    {
        measure();
    }
    }

    void setup() {
    pinMode(LED, OUTPUT);
    TACTL = ID_3 + MC_1 + TASSEL_2 + TACLR; // Set timer to up mode on SMCLK/8 (125kiHz)
    TACCTL0 = CCIE; // Enable timer interrupt
    TACCR0 = 62500 - 1; // set overflow value to remove time error (16bit counter register)
    _BIS_SR(GIE); // enabled global interrupt;
    Serial.begin(9600); 
    };
 
void loop () {
   LPM3;
};

 

The GREEN_LED is now always ON, but still nothing in Terminal output.

Here is the new picture of schematic. There you can see how all jumpers are connected.

Link to post
Share on other sites

Let me just ask you a stupid question because I'm REALLY new to all this stuff.

How should I receive Serial.print()? Via usual USB cable I use to program the chip using terminal program or via USB2UART converter usin RxD & TxD pins?

 

P.S I'm using M430G2553 chip

Link to post
Share on other sites

Edited: I was lucky and received my usb2uart converter now. It's based on CP2102 chip, if it makes any difference. I installed drivers and Windows shows it on COM11 post. I checked the code below, it's working just fine. But not the "Battery Test" code. I have no idea what to do...

 

static uint8_t sensorPin = A5;
int sensorValue = 0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  sensorValue = analogRead(sensorPin);
  Serial.println(sensorValue);
  delay(100);
}

 

Checked as well on my another code. This one works fine too.

 

#define cmdL 'L'      //UART command
#define cmdR 'R'      //UART command
 
char incomingByte;    // income data


char command;

void setup() {
  Serial.begin(9600);
}

void loop() {
  if (Serial.available() > 0) {
    incomingByte = Serial.read();
    if(incomingByte == cmdL) { 
      Serial.println("You typed L");
    }
    else if(incomingByte == cmdR) {
      Serial.println("You typed R");
    }
    else if(incomingByte > '0'){
      Serial.println("Wrong command. Please try again");
    }// ????? ??????
}
delay(100);
}
Link to post
Share on other sites

What about my my code? It just works I don't think you really need the sampling intervals to be spot on accuracy that you need a timer.

If you really want it to be better just do

 

// Very simple Arduino Lithium-ion battery capacity tester
// from electronicsblog.net
 
#define LED GREEN_LED
#define resistor 6.9
 
float capacity=0, value,voltage,current, time=0;
 
void measure (void) {
 
  value= analogRead(0);
 
  voltage=value/1024*5.0;
 
  current = voltage/resistor;
 
  capacity=capacity+current/3600;
 
  time++;
 
  Serial.print("Voltage= ");
  Serial.print(voltage);
 
  Serial.print("V Current= ");
  Serial.print(current);
 
  Serial.print("A Capacity= ");
  Serial.print(capacity);
  Serial.print("Ah ");
 
  Serial.print("Discharging time= ");
  Serial.print(time);
  Serial.print("s ");
 
  Serial.print("\n");
}
 
boolean x=false;
unsigned long ms;
 
void setup() {
 
  pinMode(LED, OUTPUT);
 
  Serial.begin(9600);
  ms = millis()+1000;
};
 
void loop () {
  digitalWrite(LED, x);
  x=!x;
  measure();
  while (millis() < ms);
  ms=millis()+1000;
};
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...