I'm trying the learn timers and pwm methods. I use the timer2 for fast pwm but i just want to use only pd3 for pwm. The code below works two pins together as pwm. How can i split these pins.
mcu is Atmega328p
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
volatile unsigned char duty_cyc_a,duty_cyc_b;
int main(void)
{
DDRB = 0b00001000;
DDRD = 0b00001000;
PORTD = 0x00;
PORTB = 0x00;
TCCR2A = 0b10100011;
TCCR2B = 0b00000001;
TCNT2 = 0;
OCR2A = 117;
OCR2B = 117;
duty_cyc_a=0;
duty_cyc_b=255;
while (1)
{
PORTB = (1<<3);
_delay_ms(1000);
PORTB &= 0;
_delay_ms(1000);
}
}
You should look at the manual, specifically the pages at the end of the section on timer2 to see how setting the registers controls the waveform and pins. The pins are called OC2A and OC2B, which are also names for PB3 and PD3.
Another tip for readable code is to use the macros that describe the pin functions, rather than combine them into one difficult binary number. If the previous writer had done that, your job would be easier.
TCCR2A = 0b10100011;
TCCR2B = 0b00000001;
should be
TCCR2A = 1 << COM2A1 | 1 << COM2B1 | 1 << WGM21 | 1 << WGM20;
TCCR2B = 1 << CS20;
Now you can see which bits the previous author set to 1. (The rest are set to 0).
Go through the tables in the register section to decide which pins you need to set in these two control registers instead. (You won't use COM2A1, and you will need to choose different WGMx pins.)
Related
I'm working with a PIC16F628A but the IDE won't accept the _delay_ms command and doesn't build/compile, I don't know what to do... here is my code:
#include <xc.h> // STANDARD INCLUDE FILE FOR MICROCHIP PRODUCTS
// uc CONFIG (START)
#pragma config FOSC = HS // 4MHz EXTERNAL CLOCK
#pragma config WTDE = ON
#pragma config PWRTE = OFF
#pragma config MCLRE = ON
#pragma config BOREN = ON
#pragma config LVP = ON
#pragma config CPD = OFF
#pragma config CP = OFF
// uC CONFIG (END)
#define _XTAL_FREQ 4000000
#define BT1 PORTA.RA0 // Button with number 1
#define BT2 PORTA.RA1 // Button with number 2
#define BT3 PORTA.RA2 // Button with number 3
#define BT4 PORTA.RA3 // Button with number 4
#define a PORTB.RB0 // "a" segment from 7-segment display
#define b PORTB.RB1 // "b" segment from 7-segment display
#define c PORTB.RB2 // "c" segment from 7-segment display
#define d PORTB.RB3 // "d" segment from 7-segment display
#define e PORTB.RB4 // "e" segment from 7-segment display
#define f PORTB.RB5 // "f" segment from 7-segment display
#define g PORTB.RB6 // "g" segment from 7-segment display
void main ()
{
TRISA = 0x01;
TRISB = 0x00;
PORTA = 0x00;
PORTB = 0x00;
while(1)
{
if (BT1 == 1)
{
a = 0x00;
b = 0x01;
c = 0x01;
d = 0x00;
e = 0x00;
f = 0x00;
g = 0x00;
_delay_ms(5000);
a = 0x00;
b = 0x00;
c = 0x00;
d = 0x00;
e = 0x00;
f = 0x00;
g = 0x00;
_delay_ms(1000);
}
}
}
Do I need to add libraries? This code worked fine in MikroC but I'm trying to learn MPLAB since it's free and seems to be an industry standard tool for embedded systems.
You don't need an extra library, but in xc8 the name of the function is__delay_ms(...) with two _.
Please remember, these functions are actually in line macros and they have maximum delay values that depend on part type and clock frequency. To get longer delays using these macros put them in a for loop. I guess __delay_ms(5000) is a little bit to much.
I am trying to make a proximity sensor using ATmega328P. I am using the onboard ADC to convert the voltage value and if it is higher than the ambient, an LED is lit.
The voltage that is being sensed is according to this circuit:
In the circuit, the VOUT is going to ADC channel 3 and should be sensed (think of the led on the right as the IR Sensor).
When the program starts, it senses 30 readings and takes their average to be used as the ambient setting. If any subsequent measurement is higher than this value, the LED should be lit.
But the LED does not light even if I place my hand above the sensor.
I have tested with just the LED to see if the IR sensor is ok. It is ok by the way.
The code for the microcontroller is as follows:
/*
* Proximity Sensor IR.c
*
* Created: 6/3/2017 2:35:33 PM
* Author : Rishav
*/
#include <avr/io.h>
#include <stdio.h>
#define F_CPU 16000000UL
#include <util/delay.h>
int calibration()
{
unsigned int sum = 0;
for (int i=0; i<30; i++)
{
ADCSRA |= (1<<ADSC);
while(!(ADCSRA & (1<<ADIF)));
ADCSRA |= (1<<ADIF);
sum += (ADCH<<8)|ADCL;
}
return (sum/30);
}
int main(void)
{
unsigned int val = 0;
ADMUX |= (0<<REFS1)|(1<<REFS0)|(0<<MUX3)|(0<<MUX2)|(1<<MUX1)|(1<<MUX0); //setting the multiplexer to ADC3
ADCSRA |= (1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);
DDRB = 0b00000010;
DDRD |= (1<<PCINT22);
PORTD |= (1<<PCINT22);
int calib_value = calibration();
while (1)
{
ADCSRA |= (1<<ADSC);
while(!(ADCSRA & (1<<ADIF)));
val = (ADCH<<8)|ADCL;
ADCSRA |= (1<<ADIF);
if (val > calib_value)
PORTB = 0b00000010;
}
}
I think there is some problem in the code. Please help.
Some things that come to mind when looking at your code:
You are really not completely initializing the ADMUX and ADCSRA registers - everything you put in there is just 'ORed'-in. (ADLAR in ADMUX is not in a defined state, for example, ADCSRA has even more undefined bits).
After setting the reference voltage source in the ADMUX register, you are supposed to wait for the chip to switch, but don't. Most probably, your first measurement in calibration will be way off. The simplest way to address this is to do one first measurement whose result you simply ignore. (or wait some ms after you have set up ADC).
You are supposed to always read ADCL before ADCH (the AVR locks the ADC for writing further results to the result register when ADCL is read until ADCH is read as well). Your current code has an undefined read order of those 2 registers.
You have to enable the ADC first and select channel and reference voltage afterwards. It is easy to skip this fact in the datasheet.
The ADC is enabled by setting the ADC Enable bit, ADEN in ADCSRA.
Voltage reference and input channel selections will not go into effect
until ADEN is set. Datasheet page 238.
I did not check all of your settings but I am pretty sure that this must be your issue.
Example order:
void init_adc()
{
ADCSRA |= (1<<ADEN); // enable ADC
ADMUX |= (1<<MUX1) | (1<<MUX0); // channel selection ADC3 - PB3
ADMUX &= ~(1<<REFS0); // VCC as reference
ADCSRA |= (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0); // setting prescaler to 128
}
As mentioned a you should read ADCL first to:
ADCL must be read first, then ADCH, to ensure that the content of the
Data Registers belongs to the same conversion
I suggest to move this part into a separate function like:
uint16_t read_adc()
{
ADCSRA |= (1<<ADSC);
while(!(ADCSRA & (1<<ADIF)));
uint8_t adcl = ADCL;
uint8_t adch = ADCH;
ADCSRA |= (1<<ADIF);
return (adch<<8) | adcl;
}
I am using an Attiny 9.
I connected a LED to PB1.
I wrote following code and upload it to the Attiny 9.
#define F_CPU 800000UL
#include <avr/io.h>
#include <util/delay.h>
#include <avr/sleep.h>
#include <avr/power.h>
#define D0 PB1
int main(void)
{
power_adc_disable();
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
TCCR0A |= (1<<WGM00)|(1<<WGM01)|(1<<COM0B1);
TCCR0B |= (1<<WGM03)|(1<<WGM02)|(1<<CS00);
DDRB |= 1<<D0;
PORTB |= 1<<D0;
_delay_ms(3000);
PORTB &= ~(1<<D0);
for (uint8_t i = 255; 247 < i; --i)
{
OCR0B = i;
_delay_ms(70);
}
for (uint8_t i= 247; -1 < i; --i)
{
OCR0B = i;
_delay_ms(4);
}
while(1){
sleep_enable();
sleep_cpu();
}
return 0;
}
I want the LED to be lighten and darken. But after I uploaded the code, the LED illuminates continuously. I also read a datasheet of Attiny 9. I followed how to use PWM. But it's not working.
What's wrong?
There are a number of issues with that code.
You're putting the processor in to power down mode, which according to the datasheet, disables the clocks and only asynchronous events can take place.
You're setting it to use fast PWM mode with OCR0A as your top, but you aren't setting OCR0A anywhere. You won't generate any compare matches if you your timer can't count high enough. It would appear from your loops that you are expecting the timer to count to 255.
You only change the duty cycle of the PWM before your while loop, so even if your PWM gets set up correctly, you won't ever be able to see your LED pulse like you want.
Here's a link giving an example of how to set up a PWM and change its duty cycles:
http://allaboutavr.com/index.php/2017/05/13/chapter-5-timers-and-pwm-pulse-width-modulation/
Ty something like this:
#include <avr/io.h>
#include <util/delay.h>
#define D0 PB1
int main(void)
{
DDRB |= 1<<D0;
TCCR0A |= (1<<WGM00)|(1<<COM0B1);
TCCR0B |= (1<<WGM02)|(1<<CS00);
while (1) {
for (uint8_t i = 0; i < 255; i++) {
OCR0B = i;
_delay_ms(4);
}
for (uint8_t i = 255; i >= 0; i--) {
OCR0B = i;
_delay_ms(4);
}
}
}
An external interrupt vector should occur when a 5v input is supplied to int0.
The interrupt should change a volatile integer flag to allow a LED to illuminate that is connected to a pin on PORTB. Compiles with no errors in Atmel studio. The problem is no change occurs when a 5v supply is sent to the int0 pin. Is this that the interrupt is not triggering?
#include <avr/io.h>
#include <stdio.h>
#define F_CPU 16000000UL
#include <util/delay.h>
#include <avr/interrupt.h>
volatile int pwm_flag=0;
int main(void)
{
DDRD &= ~(1 << DDD2); // Clear the PD2 pin
// PD2 (PCINT0 pin) is now an input
PORTD |= (1 << PORTD2); // turn On the Pull-up
// PD2 is now an input with pull-up enabled
EICRA |= (1 << ISC00)|(1 << ISC10); // set INT0 to trigger on Rising edge
EIMSK |= (1 << INT0); // Turns on INT0
sei(); // turn on interrupts
DDRB = 0xFF;
PORTB = 0x00;
while(1)
{
if(pwm_flag==1)//if flag is raised
{
PORTB = 0xFF;//turn on all pins of portb
pwm_flag=0;//reset flag to 0
}
}
}
ISR (INT0_vect)
{
/* interrupt code here */
pwm_flag =1;//raise flag
}
The problem is no change occurs when a 5v supply is sent to the int0 pin.
How do you send 5V to the INT0 pin? Your code is setting INT0 pin as an input with pullup so unless you short it to the ground it is at 5V.
Also, what do you mean by no change occurs? Is the LED on or off?
Another thing is that the line:
EICRA |= (1 << ISC00)|(1 << ISC10);
sets both INT0 and INT1 to occur on any logical change on appropriate pin. Bits with name ISC0x control INT0, bits called ISC1x are used to configure INT1. In your code you are mixing both and you end up with the following configuration:
ISC01 | ISC00 | meaning
-------------------------------------------------------------------------
0 | 1 | any logical change on INT0 generates an interrupt request
ISC11 | ISC10 | meaning
-------------------------------------------------------------------------
0 | 1 | any logical change on INT1 generates an interrupt request
If you are trying to supply 5v from a pin on PORTB to an LED then perhaps that is your problem. Most micro-controller pins can sink more current than they can give. Perhaps your LED is just not getting the current it needs?
Your wiring should be as follows:
Connect the positive leg of the LED to 5V. Connect the negative leg to a small resistor 100-500 Ohms. Connect the other leg of the resistor to the pin on PORTB.
Now you can write PORTB to 0x00 to turn on the LED or 0xFF to turn it off.
To test that the LED is working test the LED in your main loop by writing PORTB low and high with a noticeable delay in between.
If that works. Then test your ISR. If the ISR doesn't work a this point then the ISR is the problem.
Bear in mind that in your current configuration of EICRA you are triggering an interrupt on a rising edge. So if the pin is already high not interrupt will occur.
I have changed your code below so LOW is ON and HIGH is OFF.
int main(void){
DDRD &= ~(1 << DDD2); // set PD2 DDR as input
PORTD |= (1 << PORTD2); // set PD2 as input pull-up
EICRA |= (1 << ISC00)|(1 << ISC10); // set INT0 to trigger on rising edge
EIMSK |= (1 << INT0); // Turns on INT0
DDRB = 0xFF; // set PORTB as all outputs
PORTB = 0xFF; // set PORTB high
sei(); // turn on interrupts
while(1){
if(pwm_flag!=0){ // check flag
PORTB = 0x00; // set PORTB low
pwm_flag=0; // reset flag
}
}
ISR (INT0_vect){
pwm_flag = 1; // raise flag
}
I am fairly new to programming microcontrollers. I have some experience with the Arduino, but after I had nearly finished my project, I deceided to moved my current project to something cheaper and smaller. So I am now using the AVR ATmega32 with Atmel studio.
I'm trying to use the ATmega32 to communicate to a MAX7219 chip for multiplexing with an led matrix. however, I do have multiple different devices that I want to communicate with.
How can I communicate with the device without actually using the SPI pins provided on the microcontroller? I have made a test project, but there seems to be something wrong and I can't figure it out what the issue is. I think I managed to get it in test mode, because all LEDs are lit, but I can't get it to do anything after that. I can't even clear the display/shut it down. I have checked the wiring over again. Is my coding perhaps incorrect as far as pin configuration and assigning the pins? Are there any suggestions for this or a better way to write my code?
Here is the link to the MA7219 datasheet
//test
#include <avr/io.h>
int main(void)
{
DDRB = 0b00000111; // pin 1(data), 2(clock) and 3(latch) are outputs
PORTB = 0 << PINB0; // data pin 1 is low
PORTB = 0 << PINB1; // clock pin 2 is low
PORTB = 0 << PINB2; // latch pin 3 is low
uint16_t data;
data = 0b0000110000000000; // data to shift out to the max7219
//read bit
uint16_t mask;
for (mask = 0b0000000000000001; mask>0; mask <<= 1)
{
//iterate through bit mask
if (data & mask)
{ // if bitwise AND resolves to true
// send one
PORTB = 1 << PINB0;
// tick
PORTB = 1 << PINB1;
// tock
PORTB = 0 << PINB1;
}
else{ //if bitwise and resolves to false
// send 0
// send one
PORTB = 0 << PINB0;
// tick
PORTB = 1 << PINB1;
// tock
PORTB = 0 << PINB1;
}
}
PORTB = 1 << PINB2; // latch all the data
PORTB = 1 << PINB0; // data pin 1 is high
PORTB = 0 << PINB1; // clock pin 2 is low
PORTB = 0 << PINB2; // latch pin 3 is low
}
Yes, your bit-bang code has a problem in that you assign the entire value of the register each time without preserving the existing value. Hence you erase your data signal at the instant you drive your clock, violating the hold time of the receiver and resulting in unpredictable operation.
Instead of assigning pins with =, you should set them with |= or clear them with &= ~(value)
For example:
PORTB = 1 << PINB0; //drive data
// tick
PORTB |= 1 << PINB1; //clock high PRESERVING data
// tock
PORTB &= ~(1 << PINB1); //clock low
You may also need to insert a slight delay between pin operations.
Technically, given that you are already using an if for the data state, you could also re-drive the data signal with an OR in the assignment, for example
if (data & mask)
{ // if bitwise AND resolves to true
// send one
PORTB = 1 << PINB0;
// tick
PORTB = (1 << PINB1) | (1 << PINB0);
// tock
PORTB = 0 << PINB1 | (1 << PINB0);
}
else{ //if bitwise and resolves to false
// send 0
// send one
PORTB = 0 << PINB0;
// tick
PORTB = 1 << PINB1;
// tock
PORTB = 0 << PINB1;
}