Pages: [1]   Go Down
Author Topic: Help for newbie programmer :)  (Read 793 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello!

I'm a newbie in programming and in microcontrollers.
I'm trying to gather a data from a heater using a MAX6675 chip (using LadyADA's MAX6675 lib) and wanted to plot it on a graph.
I used timer/counter1 and interrupts to generate my frequencies and PWM signal to drive my output device which is my driver to my heater. I used an MOC3041 chip and a triac.

My code is:

Code:
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include "max6675.h"

#define F_CPU 16000000ul

unsigned long time;
int thermoDO=4;
int thermoCS=5;
int thermoCLK=6;

MAX6675 thermocouple(thermoCLK,thermoCS,thermoDO);
void setup()
{
  Serial.begin(9600);

  Serial.println("MAX6675 test");
 
  // wait for MAX Chip to stabilize
  delay(500);
 
  DDRB |= 1<<PORTB1; // Pin 9
  TCCR1A=0;
  TCCR1B=0;
  TCNT1=0;
 
  //This will run at a frequency of 120 Hz
  ICR1 = 16600; // TOP @ 16600 to make it 120 Hz
  OCR1A = 8300; // Initial OCR1A @ 50% Duty cycle
  TCCR1A |= 1<<WGM11 | 0<<WGM10; // Fast PWM
  TCCR1B |= 1<<WGM13 | 1<<WGM12; // ICR1 as TOP
  TCCR1B |= 0<<CS12 | 1<<CS11 | 0<<CS10; //Pre-scaled by 8
  TCCR1A |= 1<<COM1A1 | 0<<COM1A0; // Non-inverting mode
  TIMSK1 |= 1<<TOIE1;
 
  //Enable Global Interrupt
  sei();
}

void loop()
{
}

//This Interrupt Service Routine will be called everytime my
//counter hits the TOP. But we need to gather our points at 0.5 sec
//intervals.
ISR(TIMER1_OVF_vect)
{
  ++time;
  if(time>=60)
  {
    Serial.println(thermocouple.readCelsius());
    time=0;
  }
}

I didn't want to use the millis() function, so I used an increment op to get my 0.5sec interval.
Now I'm stuck in my code and don't know what to do.
I wanted to put my pulses to my OC1A pin (or pin 9) and gather temperature data for 15mins, then
turn off my pulses or turn of my driver while gathering temperature data for another 15mins. Can anyone help me with my coding?
Need help please

Thanks in advance
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49343
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I'm a newbie in programming and in microcontrollers.
Newbies and interrupts don't go together, especially when newbies don't bother researching what is, and, more importantly, what is NOT acceptable, in an ISR.

Serial.print() relies on interrupts which are disabled during an ISR. Fix that, and then come back.
Logged

Offline Offline
Sr. Member
****
Karma: 3
Posts: 250
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here It's not to hard to find
Quote
In summary,

Arduino Serial uses: Data_bits=8, Stop_bits=1, Parity=None, Flow_control=None. This is fixed and you cannot change it when using Serial. Set your PC side accordingly.
Choose a baud rate that is safe from UART buffer overflows and HardwareSerial buffer overflows for your code.
Write short ISRs to prevent UART buffer overflow.
Dangling pins with attached ISR may cause fake and random interrupts that can interfere with system clock and cause UART buffer overlow.
Write loop() carefully to check and read Serial as soon as possible to prevent HardwareSerial buffer overflow.
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17294
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
This ladies and gentlemen, is why people are starting to hate this community.

Citation available for that statement?

  The site statistics page ( http://arduino.cc/forum/index.php?action=stats ) doesn't seem to show any fall off in site usage nor new membership, to support your opinion/statement. PaulS does come from the 'tough love' school of instruction, but there is seldom fault with the accuracy of his input. And he takes on questions that many of us would struggle to try an answer, and of course he is the most prolific poster on the site by a long shot for quite some time now.

 I use to come off all hot and bothered with the many new posters that frequently seemed to want to wire their leds to output pins without current limiting resistors, and would want to argue the point when I brought it up to them. But I soon tired of the repetition as the newbies just keep coming (which is a good thing). But now so many others have taken up that particular crusade that I rarely take the bait anymore.  smiley-wink

Lefty
« Last Edit: November 22, 2012, 10:09:44 pm by retrolefty » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 29
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

well as a newb, i can tell you it is off-putting to see a reply like that. or to be told that i am stupid (a different forum).

yep, either ignore the question or provide a link or a suggestion about where to learn about it.
Logged

Offline Offline
Sr. Member
****
Karma: 3
Posts: 250
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No one said
Quote
stupid
He said that the OP should of done some reading up on this And to fix the problem of having to much code running in the ISR

Interrupts are not a fix for poor coding if you layout the flow of your code to handle
time slots it's easy to get done what you want to happen.

Here some good stuff about this http://arduino.cc/forum/index.php/topic,119801.0.html
« Last Edit: November 22, 2012, 11:40:57 pm by be80be » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 46
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 I generally stay out of these, but I have to agree the 1st reply is the kind of thing that chases people away. Come off short, if you have answered this question 100 time sure, we are all human and different but offer some assistance.  Particularly when someone is new, the better they get at this the less of a bother them become.

Don't spoon feed by any means, suggest area's of research  a few links to examples or documentation.

I can not help with the OP's question it is outside what I do.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18794
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I used timer/counter1 and interrupts to generate my frequencies and PWM signal to drive my output device which is my driver to my heater.

You can't do serial prints (successfully) inside an ISR.

http://www.gammon.com.au/interrupts
Logged


Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18794
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

^

This ladies and gentlemen, is why people are starting to hate this community.

What is the basis for this post? From someone who currently has one post to their name? Did you join just to tell us how much we are hated?

Or are you someone who has been banned for being obnoxious?

Look, it's hard work answering post after post from people who quite often don't read the "sticky" or know how to use Google. And it is made harder by people sniping at those who are trying to help.

Perhaps you can demonstrate how to answer questions better, sir.
Logged


Dee Why NSW
Offline Offline
Full Member
***
Karma: 5
Posts: 206
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm a newbie in programming and in microcontrollers.
I'm trying to gather a data from a heater using a MAX6675 chip (using LadyADA's MAX6675 lib) and wanted to plot it on a graph.

No comment on the code, it is right out of my arena, but you might find this useful for the graph.

http://www.parallax.com/tabid/393/Default.aspx

Essentially, if you can see what you want to see on the monitor, this will put it on a real-time graph.  It's a freebie and so good it can even save you money on hardware!

Quote
This ladies and gentlemen, is why people are starting to hate this community.

No, they aren't, just don't be put off by smart-arse remarks from PaulS.  He is probably not nearly as flash as he thinks he is but, when it comes to actual code, it is equally probably unwise to ignore him. Besides, there is a huge wealth of other talent here, this is the place to be, and I couldn't have gotten along without it.

Yes, I am a newbie too....
« Last Edit: November 23, 2012, 12:32:13 am by NickPyner » Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18794
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I didn't want to use the millis() function, so I used an increment op to get my 0.5sec interval.

Why don't you want to use millis? You can't do serial prints inside an ISR, so you'll have to test some sort of flag in "loop" to know when to output your data. So why not just test the time from millis?

Code:
//This will run at a frequency of 120 Hz
  ICR1 = 16600; // TOP @ 16600 to make it 120 Hz
  OCR1A = 8300; // Initial OCR1A @ 50% Duty cycle
  TCCR1A |= 1<<WGM11 | 0<<WGM10; // Fast PWM
  TCCR1B |= 1<<WGM13 | 1<<WGM12; // ICR1 as TOP
  TCCR1B |= 0<<CS12 | 1<<CS11 | 0<<CS10; //Pre-scaled by 8
  TCCR1A |= 1<<COM1A1 | 0<<COM1A0; // Non-inverting mode
  TIMSK1 |= 1<<TOIE1;

Wouldn't it be a lot easier just to do an analogWrite with a value of 127? That's a 50% duty cycle. Does the heater need to be driven at a frequency of 120 Hz?
Logged


Pages: [1]   Go Up
Jump to: