NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Master Slave communications

September 12, 2011
by kemil
kemil's Avatar

Hi all, I have a project idea which I would like to implement and it includes using two microcontrollers. The reason i need two microcontrollers is that i need 5 pwm ports which all msut be controlled individually. Ive been reading about master slave communication but im not sure on how to implement it.

I need to be able to send max 10 ANSII characters from the master to the slave mcu. The characters will come from the pc so they will need to be interpreted first by the master mcu which i can do i just need to know how to send them on. I think the communication needs to be interrupt driven as it is not a constant supply of data.

Essentially the GUI of the PC will be used to control the duty cycle of one of the pwm ports which will be used to dim a high power led.

Hope that makes sense

cheers

kemil

September 12, 2011
by bretm
bretm's Avatar

I assumes the serial port of the master will be for the PC? In that case I would consider using TWI. Search here or in the datasheet.

But if you're only doing it to get 5 PWM, I would consider using a single MCU. The atmega168 has six PWM pins. Each of the three timers can output at two different duty cycles.

September 12, 2011
by kemil
kemil's Avatar

Hi bretm,

Yes, i have no other reason to have two microcontrollers other than the need for 5 pwm outputs. I have always been under the impression that i could not set that many pins to different pwm modes. At the moment i have the following code which controls the PWM of two ports:

void pwm_init() {
ICR1 =1800;
DDRB |= (1<<PB1)|(1<<PB2);         
TCCR1A = (1<<COM1A1)|(1<<COM1B1) | (1<<WGM11);
TCCR1B = (1<<WGM13) | (1<<WGM12) | (1<<CS11);

}

I then receive data from my pc interpret it and assign its values to e.g.

 OCR1B = 1000;
 OCR1A = 1500;

How would i go about doing this for 5/6 ports? I have been trying to do it for ages but to no avail! do you have the c code at hand? would be VERY helpful..

Many Thanks

kemill

September 13, 2011
by bretm
bretm's Avatar

OCR1A and OCR1B are the two output compare registers for Timer/Counter1. There are also OCR0A and OCR0B for Timer/Counter0 and OCR2A and OCR2B for Timer/Counter2. Those other two counters are only 8-bit counters so the duty cycle resolution is limited to 256, which means you can't go up to 1800 like your example.

Post a Reply

Please log in to post a reply.

Did you know that 20 LEDs can be controlled from 11 microcontroller pins, to make a twinkling heart outline? Learn more...