I have written a code which takes two digit number from laptop and changes the PWM dutycycle to that number. It is part of a bigger requirement where I need to control motor speed over UART.
#include "io430g2553.h"
#include <stdint.h>
void PWM(uint8_t duty_cycle);
void PWM_Config();
int main( void )
{
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
WDTCTL = WDTPW + WDTHOLD;
BCSCTL1 = CALBC1_1MHZ; // Run at 1 MHz
DCOCTL = CALDCO_1MHZ; // Run at 1 MHz
PWM_Config();
PWM(5);
__delay_cycles(5000000);
PWM(15);
__delay_cycles(5000000);
PWM(25);
__delay_cycles(5000000);
PWM(50);
__delay_cycles(5000000);
PWM(25);
__delay_cycles(5000000);
PWM(15);
__delay_cycles(5000000);
PWM(5);
while(1)
{}
}
void PWM_Config()
{
P1OUT &= 0x00; // Clearing P1OUT
P1SEL |= BIT6 ;
P1SEL2 &= ~BIT6 ;
P1DIR |= BIT6; // Configuring P1.6 as Output
}
void PWM(uint8_t duty_cycle)
{
TA0CTL =0;
TA0CTL |= TACLR;
TA0CCR1 |= (duty_cycle*100);
TA0CCR0 |= 10000;
TA0CTL |= TASSEL_2 + MC_1 + ID_0;// Register TA0CTL -> SMCLK/8, Up mode
TA0CCTL1 |= OUTMOD_7 ;//Set/Reset Mode
TA0CCTL0 &= ~CCIE; // Interrupt Disabled}
The problem with the void PWM(uint8_t duty_cycle) function is that first time it generates the correct PWM at P1.6, next if it is given a value it changes PWM to that DC, but I can not go back to lower DC.
the fisrt 2 PWM functions in the code changes to correct duty cycle PWM(5),PWM(15) then the rest of PWM values do not produce desired dutycycle.
I am not able to troubleshoot where am I wrong, can any1 help?
Thanks
Seems like a stupid mistake on my part..
TA0CCR1 |= (duty_cycle*100);
instead of
TA0CCR1 = (duty_cycle*100);
Related
I have this code that I am using to play a sound effect where I used a program called wav2c to convert a .wav file to number values that I put into a header file that I use in the code to generate the sound. I currently have it programmed to play the audio upon uploading it to the Arduino with an LED being activated along with it and staying lit for just the duration of the sound effect. I am trying to program it so that the sound and LED only activate when I am pressing a button. I have the pin the button is plugged into already programmed in but I'm not sure how to have it control the audio and LED as stated above. I don't have much experience with programming or Arduino so any help is much appreciated! I am using an Arduino Mega 2560.
The code
#include <stdint.h>
#include <avr/interrupt.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#define SAMPLE_RATE 20000
#include "Test.h"
int ledPin = 2;
int speakerPin = 9; // Can be either 3 or 11, two PWM outputs connected to Timer 2
const byte pinSwitch1 = 3;
volatile uint16_t sample;
byte lastSample;
void stopPlayback()
{
digitalWrite(ledPin, LOW);
// Disable playback per-sample interrupt.
TIMSK1 &= ~_BV(OCIE1A);
// Disable the per-sample timer completely.
TCCR1B &= ~_BV(CS10);
// Disable the PWM timer.
TCCR2B &= ~_BV(CS10);
digitalWrite(speakerPin, LOW);
}
// This is called at 8000 Hz to load the next sample.
ISR(TIMER1_COMPA_vect) {
if (sample >= sounddata_length) {
if (sample == sounddata_length + lastSample) {
stopPlayback();
}
else {
if(speakerPin==11){
// Ramp down to zero to reduce the click at the end of playback.
OCR2A = sounddata_length + lastSample - sample;
} else {
OCR2B = sounddata_length + lastSample - sample;
}
}
}
else {
if(speakerPin==11){
OCR2A = pgm_read_byte(&sounddata_data[sample]);
} else {
OCR2B = pgm_read_byte(&sounddata_data[sample]);
}
}
++sample;
}
void startPlayback()
{
pinMode(speakerPin, OUTPUT);
// Set up Timer 2 to do pulse width modulation on the speaker
// pin.
// Use internal clock (datasheet p.160)
ASSR &= ~(_BV(EXCLK) | _BV(AS2));
// Set fast PWM mode (p.157)
TCCR2A |= _BV(WGM21) | _BV(WGM20);
TCCR2B &= ~_BV(WGM22);
if(speakerPin==11){
// Do non-inverting PWM on pin OC2A (p.155)
// On the Arduino this is pin 11.
TCCR2A = (TCCR2A | _BV(COM2A1)) & ~_BV(COM2A0);
TCCR2A &= ~(_BV(COM2B1) | _BV(COM2B0));
// No prescaler (p.158)
TCCR2B = (TCCR2B & ~(_BV(CS12) | _BV(CS11))) | _BV(CS10);
// Set initial pulse width to the first sample.
OCR2A = pgm_read_byte(&sounddata_data[0]);
} else {
// Do non-inverting PWM on pin OC2B (p.155)
// On the Arduino this is pin 3.
TCCR2A = (TCCR2A | _BV(COM2B1)) & ~_BV(COM2B0);
TCCR2A &= ~(_BV(COM2A1) | _BV(COM2A0));
// No prescaler (p.158)
TCCR2B = (TCCR2B & ~(_BV(CS12) | _BV(CS11))) | _BV(CS10);
// Set initial pulse width to the first sample.
OCR2B = pgm_read_byte(&sounddata_data[0]);
}
// Set up Timer 1 to send a sample every interrupt.
cli();
// Set CTC mode (Clear Timer on Compare Match) (p.133)
// Have to set OCR1A *after*, otherwise it gets reset to 0!
TCCR1B = (TCCR1B & ~_BV(WGM13)) | _BV(WGM12);
TCCR1A = TCCR1A & ~(_BV(WGM11) | _BV(WGM10));
// No prescaler (p.134)
TCCR1B = (TCCR1B & ~(_BV(CS12) | _BV(CS11))) | _BV(CS10);
// Set the compare register (OCR1A).
// OCR1A is a 16-bit register, so we have to do this with
// interrupts disabled to be safe.
OCR1A = F_CPU / SAMPLE_RATE; // 16e6 / 8000 = 2000
// Enable interrupt when TCNT1 == OCR1A (p.136)
TIMSK1 |= _BV(OCIE1A);
lastSample = pgm_read_byte(&sounddata_data[sounddata_length-1]);
sample = 0;
sei();
}
void setup()
{
pinMode( pinSwitch1, INPUT );
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
startPlayback();
}
void loop()
{
while (true);
}
The header file referenced in the code with the numeric values to create the audio.
#ifndef _HEADERFILE_H // Put these two lines at the top of your file.
#define _HEADERFILE_H // (Use a suitable name, usually based on the file name.)
const int sounddata_length=32000;
//const int sounddata_sampleRate=20000;
const unsigned char sounddata_data[] PROGMEM = {
15,1,49,0,150,0,138,0,219,255,133,0,176,0,15,1,210,
//There are many lines of more numbers in between that I cut out to save space
};
#endif // _HEADERFILE_H // Put this line at the end of your file.
The following changes will allow you to start playback whenever there is a falling edge on your switch pin. You may need to tweak to avoid switch 'bouncing'.
Firstly, add a global variable to record the last switch state:
int lastSwitchState;
Change your setup() to
void setup() {
pinMode(pinSwitch1, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
lastSwitchState = digitalRead(pinSwitch1);
}
and your loop() function to
void loop() {
delay(50);
int switchState = digitalRead(pinSwitch1);
if (switchState != lastSwitchState) {
lastSwitchState = switchState;
if (switchState == LOW) {
startPlayback();
}
}
}
Interrupts vs polling
Instead of polling the switch pin inside the main loop(), you could use interrupts. You would use attachInterrupt() to do this. Interrupts are only available on certain pins, however, and the above approach is conceptually simpler I think.
Purpose
I'm doing some timer experiment on atmega328.
I'm attempting to use every interrupt (except OVF).
Trying to make timer1 act likes 3 individual timers.
This might comes in handy when ran out of timer.
According to manual,there are 4 interrupt vectors related to timer1.
Timer1 Interrupt Vectors
My idea is making TCNT1 keep counting from 0 to 65535.
Then set the desired compare value in OCR1A,OCR1B and ICR1 registers.
When interrupts, manually add value to registers, so next interrupt will be trigger in same duration.
I wrote some codes to verify and the result seems to be ideal.
I'm considering that is this a proper way to use a timer?
or this may cause some bugs,error or efficiency issue?
Thanks!
Codes
int A_Ticks = 15624; //64us*(15624+1) = 1000ms
int B_Ticks = 23436; //1500ms
int ICR_Ticks = 31248; //2000ms
void setup()
{
Serial.begin(115200);
TCCR1A = 0x00;
TCCR1B = 0x00;
TCCR1B|= _BV(WGM13); //enable TIMER1_CAPT_vect
TCCR1B|= _BV(WGM12); //enable CTC
TCCR1B |= _BV(CS12); //prescaler = CPU clock/1024
TCCR1B &= ~_BV(CS11); //per tick = 64us
TCCR1B |= _BV(CS10);
OCR1A = A_Ticks;
OCR1B = B_Ticks;
ICR1 = ICR_Ticks;
char oldSREG = SREG;
noInterrupts();
TIMSK1 |= _BV(OCIE1A); // enable timer interrupt
TIMSK1 |= _BV(OCIE1B);
TIMSK1 |= _BV(ICIE1);
TIMSK1 |= _BV(TOIE1);
TCNT1=0;
SREG = oldSREG;
}
void loop()
{
delay(100);
}
ISR (TIMER1_COMPA_vect)
{
Serial.print(millis());
Serial.println(":COMPA");
OCR1A+=A_Ticks; //add value manually
}
ISR (TIMER1_COMPB_vect)
{
Serial.print(millis());
Serial.println(":COMPB");
OCR1B+=B_Ticks; //add value manually
}
ISR (TIMER1_OVF_vect)
{
Serial.print(millis());
Serial.println(":OVF_vect");
}
ISR (TIMER1_CAPT_vect)
{
//When interrupt into TIMER1_CAPT_vect
//TCNT will be clear to 0 due to CTC is on
//manually set it to value of ICR1 to make TCNT keep counting
TCNT1=ICR1;
ICR1+=ICR_Ticks; //add value manually
Serial.print(millis());
Serial.println(":CAPT_vect");
}
Result
999:COMPA
1499:COMPB
1999:CAPT_vect
1999:COMPA
2999:COMPA
2999:COMPB
3999:CAPT_vect
3999:COMPA
4194:OVF_vect
4499:COMPB
4999:COMPA
5999:CAPT_vect
5999:COMPA
5999:COMPB
6999:COMPA
7498:COMPB
7999:CAPT_vect
7999:COMPA
8388:OVF_vect
I am having trouble with reading data sent from my laptop to the microcontroller via UART. The transmitting part works fine - I can send data to my laptop without problems, but echoing data, or reading keyboard input data from the laptop doesn't work. I have tried to implement reading both with polling and with Rx interrupt routines, but without success. The microcontroller I am using is ATMEGA2560, and I am conntected to my laptop via FTD1232 USB to TTL serial converter.
I am using Putty as a serial comm terminal, and the Arduino IDE for programming.
I am assuming that the problem is not in the hardware based on the following - I have tried initializing different USARTs and tried to use different COM ports, as well as another USB to TTL converter, various wires, but the result is the same. I have also connected another power source to supply enough voltage to all devices.
I am trying to get at least a response by turning a LED on when Rx interrupt occurs (when I type something in the terminal).
~
#define F_CPU 16000000UL //16MHz
#define BAUD 115200UL //bps
#define BAUD_PRESCALER (((F_CPU/(BAUD*8UL)))-1) //Table 101, page 212
volatile uint8_t temp;
void UART_init(){
//Set baud rate
UBRR0H = (uint8_t)(BAUD_PRESCALER >> 8); //Higher portion of the baud rate
UBRR0L = (uint8_t)BAUD_PRESCALER; //Lower portion of the baud rate
//Enable Receiver and Transmitter
UCSR0B = (1 << RXEN0) | (1 << TXEN0);
//Set frame format: 8-bit format, 1 stop bit, no parity
UCSR0C |= (3 << UCSZ00);
UCSR0C &= ~(1 << USBS0); //clear for 1 stop bit
//Set Double Speed Asynchronous mode
UCSR0A |= (1 << U2X0);
UCSR0B |= (1 << RXCIE0); //Enables interrupt rxc
}
uint8_t UART_getchar(){
//Wait for RXC flag
while(!(UCSR0A & (1 << RXC0))){};
return UDR0;
}
//Arduino setup()
void setup(){
pinMode(8, OUTPUT);
UART_init();
sei();
}
//Arduino loop()
void loop(){
delay(500);
UART_putchar('a');
}
ISR(USART0_RX_vect){
temp = UDR0;
digitalWrite(8, HIGH);
}
It is a lenghty post, but I tried to be specific and also post the whole code. The datasheet I am using for the ATMEGA2560 can be found here.
My Putty settings:
COM Port Settings
I've just tested it on my Mega board and it's working fine without interrupts enabled (simple loopback):
#define BAUD 115200UL
#define BAUD_PRESCALER (((F_CPU/(BAUD*8UL)))-1)
volatile uint8_t temp;
void UART_init(){
//Set baud rate
UBRR0H = (uint8_t)(BAUD_PRESCALER >> 8); //Higher portion of the baud rate
UBRR0L = (uint8_t)BAUD_PRESCALER; //Lower portion of the baud rate
//Enable Receiver and Transmitter
UCSR0B = (1 << RXEN0) | (1 << TXEN0);
//Set frame format: 8-bit format, 1 stop bit, no parity
UCSR0C |= (3 << UCSZ00);
UCSR0C &= ~(1 << USBS0); //clear for 1 stop bit
//Set Double Speed Asynchronous mode
UCSR0A |= (1 << U2X0);
// UCSR0B |= (1 << RXCIE0); //Enables interrupt rxc
}
uint8_t UART_getchar(){
//Wait for RXC flag
while(!(UCSR0A & (1 << RXC0))){};
return UDR0;
}
void UART_putchar(uint8_t data){
//Do nothing until UDR is ready
while(!(UCSR0A &(1 << UDRE0))){};
UDR0 = data;
}
void UART_putstring(uint8_t *data){
while(*data != 0x00){
UART_putchar(*data);
data++;
}
}
//Arduino setup()
void setup(){
pinMode(8, OUTPUT);
UART_init();
sei();
}
//Arduino loop()
void loop(){
UART_putchar(UART_getchar());
}
/*ISR(USART0_RX_vect){
temp = UDR0;
digitalWrite(8, HIGH);
}*/
RX interrupt must be disabled, otherwise Serial interrupt vector is taking place and polled get char can't work as characters are readed by ISR
I have been studying microcontrollers so far and I gone through LCD and understood how it worked in 8 bits mode but 1 thing I don't understand is how to work with it in 4 bits mode and whatever I try in code I cant ever make it and I searched almost all the sites available on the internet to explain this topic but still didn't get it so I would very much appreciate if someone could spend some of his time explaining this to me thanks in advance.
#user3674628: By now you must have had known how to interface in 4-bit modes. Though you didnt specify the compiler you use here is an example I have been using with mplabx with xC8 compiler. It works pretty well. Do not forget to adjust the configuration bits on mplabx. Success!
// PIC18F4550 Configuration Bit Settings
// 'C' source line config statements
#include <xc.h>
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
// CONFIG1L
#pragma config PLLDIV = 5
#pragma config CPUDIV = OSC1_PLL2
#pragma config USBDIV = 2
// CONFIG1H
#pragma config FOSC = INTOSCIO_EC //this uses internal oscillator
#pragma config FCMEN = OFF
#pragma config IESO = OFF
// CONFIG2L
#pragma config PWRT = OFF
#pragma config BOR = OFF
#pragma config BORV = 3
#pragma config VREGEN = OFF
// CONFIG2H
#pragma config WDT = OFF
#pragma config WDTPS = 32768
// CONFIG3H
#pragma config CCP2MX = ON
#pragma config PBADEN = OFF
#pragma config LPT1OSC = OFF
#pragma config MCLRE = OFF
// CONFIG4L
#pragma config STVREN = ON
#pragma config LVP = OFF
#include <p18f4550.h>
#include <stdio.h>
#include <stdlib.h>
#include <plib/xlcd.h>
/* DATA_PORT defines the port to which the LCD data lines are connected */
#define DATA_PORT PORTB //for 4-bit mode connect D4-D7 of lcd to B0-B3 of lcd
#define TRIS_DATA_PORT TRISB
/* This defines the port where the control lines are connected.
* These are just samples, change to match your application.
* you may omit this part because its in y the compiler.
*/
#define RW_PIN LATBbits.LATB6
#define TRIS_RW TRISBbits.TRISB6
#define RS_PIN LATBbits.LATB5
#define TRIS_RS TRISBbits.TRISB5
#define E_PIN LATBbits.LATB4
#define TRIS_E TRISBbits.TRISB4
#define _XTAL_FREQ 4000000 // SET THIS TO SUIT YOUR FREQUENCY
void Wait(unsigned int delay)//delay for 100ms
{
for(;delay;delay--)
__delay_us(100);
}
//initialise the lcd
void LCDInit(){
OpenXLCD(FOUR_BIT & LINES_5X7 & CURSOR_ON & BLINK_OFF);
while(BusyXLCD());
WriteCmdXLCD(SHIFT_DISP_LEFT);
while(BusyXLCD());
}
void main(void)
{
ADCON1 = 0xF; // No analog, all digital i/o
TRISB = 0x0;
LCDInit()//initialise lcd
putrsXLCD( " Thanks God" );//display on the lcd
while(BusyXLCD());
WriteCmdXLCD(0xC0);// go to the next line on the lcd
while(BusyXLCD());
putrsXLCD( " I made it" );//display on the lcd
while(BusyXLCD());
while(1);
}
void DelayFor18TCY(void){
Delay10TCYx(20);
}
void DelayPORXLCD(void){
Delay1KTCYx(30);
}
void DelayXLCD(void){
Delay1KTCYx(10);
}
Have you tried these?
http://learningmsp430.wordpress.com/2013/11/16/16x2-lcd-interfacing-in-4-bit-mode/
http://www.spickles.org/projects/4-bit-lcd/
The first is pretty informative, and the second has example source code.
For 4-bit interface data, only four bus lines (DB4 to DB7) are used for transfer. Bus lines DB0 to DB3
are disabled. The data transfer between the HD44780U and the MPU is completed after the 4-bit data
has been transferred twice. As for the order of data transfer, the four high order bits (for 8-bit
operation, DB4 to DB7) are transferred before the four low order bits (for 8-bit operation, DB0 to
DB3).
The busy flag must be checked (one instruction) after the 4-bit data has been transferred twice. Two
more 4-bit operations then transfer the busy flag and address counter data.
For 8-bit interface data, all eight bus lines (DB0 to DB7) are used.
Here is an example on how it works:
#include <msp430g2553.h>
#define DR P1OUT = P1OUT | BIT4 // define RS high
#define CWR P1OUT = P1OUT & (~BIT4) // define RS low
#define READ P1OUT = P1OUT | BIT5 // define Read signal R/W = 1 for reading
#define WRITE P1OUT = P1OUT & (~BIT5) // define Write signal R/W = 0 for writing
#define ENABLE_HIGH P1OUT = P1OUT | BIT6 // define Enable high signal
#define ENABLE_LOW P1OUT = P1OUT & (~BIT6) // define Enable Low signal
void data_write(void)
{
ENABLE_HIGH;
delay(2);
ENABLE_LOW;
}
void data_read(void)
{
ENABLE_LOW;
delay(2);
ENABLE_HIGH;
}
void check_busy(void)
{
P1DIR &= ~(BIT3); // make P1.3 as input
while((P1IN&BIT3)==1)
{
data_read();
}
P1DIR |= BIT3; // make P1.3 as output
}
void send_command(unsigned char cmd)
{
check_busy();
WRITE;
CWR;
P1OUT = (P1OUT & 0xF0)|((cmd>>4) & 0x0F); // send higher nibble
data_write(); // give enable trigger
P1OUT = (P1OUT & 0xF0)|(cmd & 0x0F); // send lower nibble
data_write(); // give enable trigger
}
void send_data(unsigned char data)
{
check_busy();
WRITE;
DR;
P1OUT = (P1OUT & 0xF0)|((data>>4) & 0x0F); // send higher nibble
data_write(); // give enable trigger
P1OUT = (P1OUT & 0xF0)|(data & 0x0F); // send lower nibble
data_write(); // give enable trigger
}
void send_string(char s)
{
while(s)
{
send_data(*s);
s++;
}
}
void lcd_init(void)
{
P1DIR |= 0xFF;
P1OUT &= 0x00;
send_command(0x33);
send_command(0x32);
send_command(0x28); // 4 bit mode
send_command(0x0E); // clear the screen
send_command(0x01); // display on cursor on
send_command(0x06); // increment cursor
send_command(0x80); // row 1 column 1
}
#include “lcd.h”
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // stop watchdog timer
lcd_init();
send_string(“Hello”);
send_command(0xC0);
send_string(“World”);
while(1){}
}
When I compile this code ;
#include <avr/interrupt.h>
#include <avr/io.h>
#include <util/delay.h>
volatile int state_Led = LOW;
int SerialVal_;
void setup()
{
#define USART_BAUDRATE 9600
#define UBRR_VALUE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)
UBRR0H = (uint8_t)(UBRR_VALUE>>8);
UBRR0L = (uint8_t)UBRR_VALUE;
UCSR0B |= (1 << RXEN0) | (1 << TXEN0) | (1 << RXCIE0); // Turn on the transmission, reception, and Receive interrupt
UCSR0C |= (1<<UCSZ01)|(1<<UCSZ00);
interrupts();
}
int c = 0;
int b;
void loop()
{
if(Serial.available() > 0); // Error Line
}
ISR(USART_RX_vect)
{
cli();
while(!(UCSR0A&(1<<RXC0))){};
c= UDR0; // clear the USART interrupt
UDR0 = c + 0x0A;
sei();
}
I get the error of "C:\Program Files\Arduino\hardware\arduino\cores\arduino/HardwareSerial.cpp:115: multiple definition of `__vector_18'"
To sum up, if I use interrupt with USART interrupt vector I can't use simple Serial.xxx class methods for a reason that I don't know. I know lots of people are having the same problem. If someone berings the solution, that would be great. Thanks in advance