Squirrels have fuzzy tails.
0 Members and 1 Guest are viewing this topic.
waiting to find out what your IDE is like )
#include "sys/atmega328P.h"#include "a2d.h"#include "uart.h"#include "pwm.h"#include "servos.h"#include "rprintf.h"#include "spi.h"#define Speed ADC_NUMBER_TO_CHANNEL(0)uint16_t currValue = 0;int outputVal = 0;int index = 0; //used to keep track of incoming byte positionint rxdata[4]; //create the array to be used as the bufferint cmd[4]; //To store the command for processingboolean execute = FALSE; //Execute?int AOut = 111;void myReceiveCallBack(unsigned char data, void* device){ //Recieve interrupt, called with each incoming byte rxdata[index] = data; //Puts the incoming data into the rxdata array at position ch index++; //increment index if(index == 4){ index = 0; cmd[0] = rxdata[0]; cmd[1] = rxdata[1]; cmd[2] = rxdata[2]; cmd[3] = rxdata[3]; execute = TRUE; }}void appInitHardware(void) { uartInit(UART0, (BAUD_RATE)115200); uartAttach(UART0, &myReceiveCallBack, NULL); rprintfInit(&uart0SendByte); rprintfStr("Hello"); pwmInitHertz(D3, 500, 50, null); pin_make_input(D7,FALSE); }// Initialise the softwareTICK_COUNT appInitSoftware(TICK_COUNT loopStart){ return 0;}// This is the main loopTICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopStart) { if(execute == TRUE){ execute = FALSE; if(cmd[0] == 'a'){ //Channel A if(cmd[1] >= '0' && cmd[1] <= '9' && cmd[2] >= '0' || cmd[2] <= '9' && cmd[3] >= '0' || cmd[3] <= '9'){ cmd[1] -= '0'; cmd[2] -= '0'; cmd[3] -= '0'; AOut = cmd[1] * 100 + cmd[2] * 10 + cmd[3]; } } } if(AOut > 100) currValue = a2dConvert10bit(Speed) / 10; else currValue = AOut; pwmSetDutyCycle(D3,(currValue)); spiDeviceSendByte(&sevenSegment,numOut[(currValue/10)]); if (pin_is_low(D7)){ rprintfStr("Button"); index = 0; } return 5000;}
// Set the duty cyclevoid pwmSetDutyCycle(const IOPin* pin, DUTY_CYCLE duty){ const TimerCompare* channel = compareFromIOPin(pin); if(channel){ const Timer* timer = compareGetTimer(channel); uint32_t top = timerGetTOP(timer); // Limit the duty cycle if(duty>100) duty=100; uint16_t delay = interpolateU(duty, 0,100, 0,top); // Change the duty cycle compareSetThreshold(channel,delay); }}
uint16_t delay = interpolateU(duty, 0,100, 0,top);
One small warning: I have found that setting the PWM compare value to 0 does *not* cause the PWM to output constant-low. It will output a very short spike each time the timer cycles. To make it output constant-low, you have to turn off the output compare unit.This may or may not actually matter in this case, but I thought I'd point it out!