Skip to content

Instantly share code, notes, and snippets.

@JHeld07
Created October 6, 2017 04:06
Show Gist options
  • Save JHeld07/6f1eb6564d823747b03b8d3b138c6ff1 to your computer and use it in GitHub Desktop.
Save JHeld07/6f1eb6564d823747b03b8d3b138c6ff1 to your computer and use it in GitHub Desktop.
Test Fixture Code
/*
* Test_1.c
*
* Created on: Oct 5, 2017
* Author: heldjj
* Inputs: 1x pushbutton w/pullup resistor
* Outputs:3 indicator leds. (1x pushbutton indicator, 1x PWM brightness display, 1x pulse display)
* Purpose: To be able to control the ouput pulse length and fire only once per button press.
*
*/
#include <avr/io.h>
#include <inttypes.h>
#include <MSOE/delay.c>
#include <MSOE/bit.c>
#include <MSOE/lcd.c>
#include <avr/interrupt.h>
//Function prototypes
void Actuator_Fire(int Actuator_Number);
float Actuator_PWM(int Actuator_Power);
//Global Variables
int Actuator_Number=0;
float Actuator_Power;
int Time=0; // flag for ISR use
int TimeOn;
int TimeOff;
int Button;
int Fire;
int main(void)
{
//Timer1
TCCR1A |= (1<<COM1A1)|(1<<COM1B1)|(1<<WGM11)|(1<<WGM10); // fast pwm, mode 7, 0x03ff(1024) top, 10 bit
TCCR1B |= (1<<WGM12)|(1<<CS11); // Pre-scaler 5 top
//setting up timer0
TCCR0A|=0x02; // WGM01 = HIGH, CTC mode prescaler 8 top required of 52, normal pin operation for ocr1a&b (disconnected)
TCCR0B|=0x02; // CS01 = HIGH
DDRC|=0x01; // OUTPUT PINC0 initally set to output
PORTC&=(~0x01); // Set Output low initially
OCR0A=26; // PRODUCES A 37.73K HZ SQUARE WAVE
TIMSK0|=0x02; // ociea intrrupt mask enabled
//PWM Duty Cycle Settings
OCR1B=1000; // Duty cycle /1024 Pin B2
//Pin output settings
DDRB |=0x04; //0000 0110 Pin B2 output Left on for reference brightness
DDRB &=~(0x02); //0000 0110 Pin B1 input
DDRC &=(~0x01); // Set Pin C0 as input
PORTC &=~(0x01); // Turn off pull up resistor
DDRC |=0x20; //0010 0000 Set pin C5 as output
PORTC &=~(0x20); //1101 1111 Set Pin C5 to low
sei(); // global interrupt enable
while(1)
{
float Power;
Button=(PINC&0x01);
//************* Enter a power value from 1-100 representing power % **********************
Actuator_Power =100;//<-----------------
Actuator_Number=0;
if (Button==1)
{
PORTC |=0x20; //1101 1111 Set Pin C5 to low
}
else
{
PORTC &=~(0x20); //1101 1111 Set Pin C5 to low
}
TimeOn=5000;
//*************** Enter PWM signal on time here- Change the 5000 value to change when the signal is turned off***************************
TimeOff=(TimeOn+5000);//<---------------
/* Each full cycle = 52.6313E-6 seconds 38.8kHz signal
*Sec # of cycles
0.010 380
0.020 760
0.030 1140
0.040 1520
0.050 1900
0.060 2280
0.070 2660
0.080 3040
0.090 3420
0.100 3800
0.150 5700
0.200 7600
0.250 9500
0.300 11400
0.350 13300
0.400 15200
0.450 17100
0.500 19000
*/
Power=Actuator_PWM(Actuator_Power);
OCR1A=Power;
Actuator_Fire(Actuator_Number);
}//end while
}//end main
void Actuator_Fire(int Actuator_Number)
{
switch (Actuator_Number)//Control which actuator is firing
{
case '0':
break;
case '1':
break;
case '2':
break;
case '3':
break;
case '4':
break;
case '5':
break;
default:
TimeOn=10;
}// end switch
}// end Actuator_Fire
float Actuator_PWM(int Actuator_Power)
{
float Power;
Power=((Actuator_Power/100)*1000);
return Power;
}// End Actuator_PWM
// INT0 ISR
ISR (TIMER0_COMPA_vect) // enter interrupt when Timer0 compare match B is true
{
Time++;
// Each full cycle = 52.6313E-6 seconds
if ((Button==1)&&(Fire==0))
{
if(Time==TimeOn) // Value divided by 2 = # of cycles
{
DDRB|= 0x02; //0000 0010 Set pin B1 to output
}
else if (Time==TimeOff)
{
DDRB&=~(0x02); // 0000 0010 Set pin B1 to input
Time=0;
Fire=1;
}
}
else if (Button==0)
{
DDRB&=~(0x02); // 0000 0010 Set pin B1 to input This ensures that the output is off when the button is released
Fire=0;
Time=0;
}
}
/*
#include <avr/io.h>
#include <inttypes.h>
#include <MSOE/delay.c>
#include <MSOE/bit.c>
#include <MSOE/lcd.c>
#include <avr/interrupt.h>
//Function prototypes
void Actuator_Fire(int Actuator_Number);
float Actuator_PWM(int Actuator_Power);
//Global Variables
int Actuator_Number=0;
float Actuator_Power;
int Time=0; // flag for ISR use
int TimeOn;
int TimeOff;
int Button;
int Fire;
int main(void)
{
//Timer1
TCCR1A |= (1<<COM1A1)|(1<<COM1B1)|(1<<WGM11)|(1<<WGM10); // fast pwm, mode 7, 0x03ff(1024) top, 10 bit
TCCR1B |= (1<<WGM12)|(1<<CS11); // Pre-scaler 5 top
//Timer0
// TCCR0A |= (1<<COM0A1)|(1<<COM0B1)|(1<<WGM01)|(1<<WGM00); // fast pwm, mode 3, 0x0ff(255) top, 10 bit
// TCCR0B |= (1<<WGM02)|(1<<CS11)|(1<<CS10); // Pre-scaler 1024 top
//setting up timer0
TCCR0A|=0x02; // WGM01 = HIGH, CTC mode prescaler 8 top required of 52, normal pin operation for ocr1a&b (disconnected)
TCCR0B|=0x02; // CS01 = HIGH
DDRC|=0x01; // OUTPUT PINC0 initally set to output
PORTC&=(~0x01); // Set Output low initially
OCR0A=26; // PRODUCES A 37.73K HZ SQUARE WAVE
TIMSK0|=0x02; // ociea intrrupt mask enabled
//PWM Duty Cycle Settings
//OCR1A=50; //Duty cycle /1024 Pin B1
OCR1B=1000; // Duty cycle /1024 Pin B2
// OCR0A=0; // Duty cycle /255 Pin D6
// OCR0B=0; // Duty cycle /255 Pin D5
//Pin output settings
DDRB |=0x04; //0000 0110 Pin B2 output Left on for reference brightness
// DDRB |=0x02; //0000 0110 Pin B1 output
// DDRD |=0x60; //0110 0000 Pin D9&10 output
// Going to want to turn pins off initially then on for the period of fire time.
// DDRB &=~(0x04); //0000 0110 Pin B2 input
DDRB &=~(0x02); //0000 0110 Pin B1 input
// DDRD &=~(0x60); //0110 0000 Pin D5 & D6 input
DDRC &=(~0x01); // Set Pin C0 as input
PORTC &=~(0x01); // Turn off pull up resistor
DDRC |=0x20; //0010 0000 Set pin C5 as output
PORTC &=~(0x20); //1101 1111 Set Pin C5 to low
sei(); // global interrupt enable
while(1)
{
float Power;
Button=(PINC&0x01);
//************* Enter a power value from 1-100 representing power % **********************
Actuator_Power =100;
Actuator_Number=0;
if (Button==1)
{
PORTC |=0x20; //1101 1111 Set Pin C5 to low
}
else
{
PORTC &=~(0x20); //1101 1111 Set Pin C5 to low
}
//TimeOn=12000;
// TimeOff=13000;
TimeOn=5000;
TimeOff=(TimeOn+5000);
Power=Actuator_PWM(Actuator_Power);
OCR1A=Power;
Actuator_Fire(Actuator_Number);
}//end while
}//end main
void Actuator_Fire(int Actuator_Number)
{
switch (Actuator_Number)//Control which actuator is firing
{
case '0':
break;
case '1':
break;
case '2':
break;
case '3':
break;
case '4':
break;
case '5':
break;
default:
TimeOn=10;
}// end switch
// return TimeOn;
}// end Actuator_Fire
float Actuator_PWM(int Actuator_Power)
{
float Power;
Power=((Actuator_Power/100)*1000);
return Power;
}// End Actuator_PWM
// INT0 ISR
ISR (TIMER0_COMPA_vect) // enter interrupt when Timer0 compare match B is true
{
Time++;
// Each full cycle = 52.6313E-6 seconds
//int Fire=0;
if ((Button==1)&&(Fire==0))
{
if(Time==TimeOn) // Value divided by 2 = # of cycles
{
DDRB|= 0x02; //0000 0010 Set pin B1 to output
}
else if (Time==TimeOff)
{
DDRB&=~(0x02); // 0000 0010 Set pin B1 to input
Time=0;
Fire=1;
}
}
else if (Button==0)
{
DDRB&=~(0x02); // 0000 0010 Set pin B1 to input This ensures that the output is off when the button is released
Fire=0;
Time=0;
}
}
*
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment