Skip to content

Instantly share code, notes, and snippets.

@rajeevs1992
Created January 22, 2013 14:39
Show Gist options
  • Save rajeevs1992/4595109 to your computer and use it in GitHub Desktop.
Save rajeevs1992/4595109 to your computer and use it in GitHub Desktop.
#include<msp430.h>
void init_adc()
{
ADC10CTL0|=ADC10ON|SREF_0|ADC10SHT_2;
ADC10CTL1|=INCH_4|SHS_0|ADC10DIV_0|ADC10SSEL_0|CONSEQ_0;
ADC10AE0=BIT4;
ADC10CTL0|=ENC;
}
void start()
{
ADC10CTL0|=ADC10SC;
}
int busy()
{
return ADC10CTL0 & ADC10BUSY;
}
void delay(int i)
{
while(i--);
}
void pwm(int d)
{
P1OUT=0x20;
delay(d);
P1OUT=0x00;
delay(1000-d);
}
main()
{
init_adc();
P1DIR=0x20;
P1OUT=0x00;
while(1)
{
start();
while(busy());
if(ADC10MEM>1000)
P1OUT=0x20;
else if(ADC10MEM>950)
pwm(950);
else if(ADC10MEM>900)
pwm(900);
else if(ADC10MEM>850)
pwm(850);
else if(ADC10MEM>800)
pwm(800);
else if(ADC10MEM>700)
pwm(750);
else if(ADC10MEM>650)
pwm(700);
else if(ADC10MEM>600)
pwm(650);
else if(ADC10MEM>550)
pwm(600);
else if(ADC10MEM>500)
pwm(550);
else if(ADC10MEM>450)
pwm(500);
else if(ADC10MEM>400)
pwm(450);
else if(ADC10MEM>350)
pwm(400);
else if(ADC10MEM>300)
pwm(350);
else if(ADC10MEM>250)
pwm(300);
else if(ADC10MEM>200)
P1OUT=0x00;
delay(1000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment