Combination of PWM and TFT display

Hi to all!

Our project consists out of two functions (PWM + display) and we are using the ATmega 2560.

Running PWM and display seperatly works.
However putting PWMInit and TFTInit together doesn´t work and apparently loop is not executed...
We changed the order of functions and many other things...

When commenting out PWMInit it works so maybe you guys know any reason for that?

Appreciate any help!!

Best wishes from Germany,
Phil

#include <SPI.h>
#include <TFT.h>

void TFTinit();
void TFToutput(char* output,int Size,char C,int X, int Y);
void PWMinit();

#define CS 22
#define RESET 23
#define DC 24


TFT screen = TFT(CS, DC, RESET);

void setup() {
pinMode(40,INPUT);
pinMode(41,OUTPUT);
TFTinit();
pinMode(11,OUTPUT);   //set PWM Pin as output
// PWMinit();           //initialize PWM 
}


void loop() {

 digitalWrite(41,HIGH);
 TFToutput("hello",3,'j',0,0);

}



// initialize display
void TFTinit(){
 screen.begin();
 screen.background(0,0,0);
 screen.setTextSize(4);
 screen.stroke(0,0,255);
 screen.text("Mind-",10,20);
 screen.text("Racing",10,70);
 
}

// output,size, erase yes/no,x Pos, y Pos
void TFToutput(char* output,int Size,char C,int X, int Y){

  if (C=='j'){
  screen.background(0,0,0);
  screen.setTextSize(Size);
  screen.text(output,X,Y);  
  }
  if (C=='n'){
  screen.setTextSize(Size);
  screen.text(output,X,Y);  
  }
}

void PWMinit(){
 noInterrupts();    // initially deactivating all interrupts
 TCCR1A=0; // set TimerCounter 1 Control Register A to zero
 TCCR1B=0; // set TimerCounter 1 Control Register B to zero
 TCNT1=34286;    // initialize TimerCounter 1 to zero
 
 OCR1A=31250;   // set Output Compare Register 1A; 0,5*(16.000.000 Hz / 256) = 31250

 TCCR1A|=(1<<COM1A1); // Compare Output Mode, Fast PWM: inverting mode (Bit 7)
 TCCR1A|=(1<<COM1A0); // Compare Output Mode, Fast PWM: inverting mode (Bit 6)
 TCCR1A|=(1<<WGM10);  // set WGMn0(PWMn0) to 1 for Mode: 5 Fast PWM, 8-bit; TOP:0x00FF,Update of OCRnX at Bottom; TOVnFlag Set on TOP (Bit 0)
  
 TCCR1B|=(1<<CS12);   // set prescaler to 256 (Bit 2) 
 TCCR1B|=(1<<WGM12);  // set WGMn2(CTCn) to 1 for Mode: 5 Fast PWM, 8-bit; TOP:0x00FF,Update of OCRnX at Bottom; TOVnFlag Set on TOP (Bit 3)
 
 TIMSK1|=(1<<OCIE1A); // activate Timer Compare Interrupt
 interrupts();    // activate all interrupts
}

Hi to all!

Our project consists out of two parts (one PWM and one display).

Running PWM and display seperatly works.
However putting PWMInit and TFTInit together doesn´t work and apparently loop is not executed...
We changed the order of functions and many other things...

When commenting out PWMInit it works so maybe you guys know any reason for that?

Appreciate any help!!

Best wishes,
Phil

tftpwm_loop.ino (1.21 KB)

Hi to all!

Our project consists out of two functions (PWM + display) and we are using the ATmega 2560.

Running PWM and display seperatly works.
However putting PWMInit and TFTInit together doesn´t work and apparently loop is not executed...
We changed the order of functions and many other things...

When commenting out PWMInit it works so maybe you guys know any reason for that?

#include <SPI.h>
#include <TFT.h>

void TFTinit();
void TFToutput(char* output,int Size,char C,int X, int Y);
void PWMinit();

#define CS 22
#define RESET 23
#define DC 24


TFT screen = TFT(CS, DC, RESET);

void setup() {
pinMode(40,INPUT);
pinMode(41,OUTPUT);
TFTinit();
pinMode(11,OUTPUT);   //set PWM Pin as output
PWMinit();           //initialize PWM 
}


void loop() {

 digitalWrite(41,HIGH);
 TFToutput("hello",3,'j',0,0);

}



// initialize display
void TFTinit(){
 screen.begin();
 screen.background(0,0,0);
 screen.setTextSize(4);
 screen.stroke(0,0,255);
 screen.text("Mind-",10,20);
 screen.text("Racing",10,70);
 
}

// output,size, erase yes/no,x Pos, y Pos
void TFToutput(char* output,int Size,char C,int X, int Y){

  if (C=='j'){
  screen.background(0,0,0);
  screen.setTextSize(Size);
  screen.text(output,X,Y);  
  }
  if (C=='n'){
  screen.setTextSize(Size);
  screen.text(output,X,Y);  
  }
}

void PWMinit(){
 noInterrupts();    // initially deactivating all interrupts
 TCCR1A=0; // set TimerCounter 1 Control Register A to zero
 TCCR1B=0; // set TimerCounter 1 Control Register B to zero
 TCNT1=34286;    // initialize TimerCounter 1 to zero
 
 OCR1A=31250;   // set Output Compare Register 1A; 0,5*(16.000.000 Hz / 256) = 31250

 TCCR1A|=(1<<COM1A1); // Compare Output Mode, Fast PWM: inverting mode (Bit 7)
 TCCR1A|=(1<<COM1A0); // Compare Output Mode, Fast PWM: inverting mode (Bit 6)
 TCCR1A|=(1<<WGM10);  // set WGMn0(PWMn0) to 1 for Mode: 5 Fast PWM, 8-bit; TOP:0x00FF,Update of OCRnX at Bottom; TOVnFlag Set on TOP (Bit 0)
  
 TCCR1B|=(1<<CS12);   // set prescaler to 256 (Bit 2) 
 TCCR1B|=(1<<WGM12);  // set WGMn2(CTCn) to 1 for Mode: 5 Fast PWM, 8-bit; TOP:0x00FF,Update of OCRnX at Bottom; TOVnFlag Set on TOP (Bit 3)
 
 TIMSK1|=(1<<OCIE1A); // activate Timer Compare Interrupt
 interrupts();    // activate all interrupts
}

so maybe you guys know any reason for that?

You need to comment EVERY line in PWMInit(), so we can see whether you are setting that up right.

You need to explain WHY you are setting PWM up in a non-standard way.

Threads merged.

PaulS:
You need to comment EVERY line in PWMInit(), so we can see whether you are setting that up right.

You need to explain WHY you are setting PWM up in a non-standard way.

Could you explain the non-standard way of setting PWM up refering to my comments?

MindracingAux:
Could you explain the non-standard way of setting PWM up refering to my comments?

No. You modified your post to add comments, making me look stupid for telling you that you needed to add comments. I don't try to help people that try to make me look stupid. I am perfectly capable of doing that on my own.