baileychic
Advanced Member level 3
- Joined
- Aug 2, 2017
- Messages
- 728
- Helped
- 56
- Reputation
- 112
- Reaction score
- 57
- Trophy points
- 28
- Activity points
- 7,033
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 /******************************************************* This program was created by the CodeWizardAVR V3.28 Automatic Program Generator © Copyright 1998-2016 Pavel Haiduc, HP InfoTech s.r.l. [url]http://www.hpinfotech.com[/url] Project : Version : Date : 8/23/2017 Author : Company : Comments: Chip type : ATtiny25 AVR Core Clock frequency: 8.000000 MHz Memory model : Tiny External RAM size : 0 Data Stack size : 32 *******************************************************/ #include <io.h> #include <delay.h> // Declare your global variables here int i = 0; void main(void) { // Declare your local variables here // Crystal Oscillator division factor: 1 #pragma optsize- CLKPR=(1<<CLKPCE); CLKPR=(0<<CLKPCE) | (0<<CLKPS3) | (0<<CLKPS2) | (0<<CLKPS1) | (0<<CLKPS0); #ifdef _OPTIMIZE_SIZE_ #pragma optsize+ #endif // Input/Output Ports initialization // Port B initialization // Function: Bit5=Out Bit4=Out Bit3=Out Bit2=In Bit1=Out Bit0=Out DDRB=(1<<DDB5) | (1<<DDB4) | (1<<DDB3) | (0<<DDB2) | (1<<DDB1) | (1<<DDB0); // State: Bit5=0 Bit4=0 Bit3=0 Bit2=P Bit1=0 Bit0=0 PORTB=(0<<PORTB5) | (0<<PORTB4) | (0<<PORTB3) | (1<<PORTB2) | (0<<PORTB1) | (0<<PORTB0); // Timer/Counter 0 initialization // Clock source: System Clock // Clock value: Timer 0 Stopped // Mode: Fast PWM top=0xFF // OC0A output: Non-Inverted PWM // OC0B output: Disconnected TCCR0A=(1<<COM0A1) | (0<<COM0A0) | (0<<COM0B1) | (0<<COM0B0) | (1<<WGM01) | (1<<WGM00); TCCR0B=(0<<WGM02) | (0<<CS02) | (0<<CS01) | (0<<CS00); TCNT0=0x00; OCR0A=0x00; OCR0B=0x00; // Timer/Counter 1 initialization // Clock source: System Clock // Clock value: 15.625 kHz // Mode: PWMA top=OCR1C // OC1A output: OC1A=PWM, /OC1A=/PWM // OC1B output: Disconnected // Timer Period: 9.984 ms // Output Pulse(s): // OC1A Period: 9.984 ms Width: 5.0242 ms // Timer1 Overflow Interrupt: Off // Compare A Match Interrupt: Off // Compare B Match Interrupt: Off PLLCSR=(0<<PCKE) | (0<<PLLE) | (0<<PLOCK); TCCR1=(0<<CTC1) | (1<<PWM1A) | (0<<COM1A1) | (1<<COM1A0) | (1<<CS13) | (0<<CS12) | (1<<CS11) | (0<<CS10); GTCCR=(0<<TSM) | (0<<PWM1B) | (0<<COM1B1) | (0<<COM1B0) | (0<<PSR1) | (0<<PSR0); TCNT1=0x00; OCR1A=0x4E; OCR1B=0x00; OCR1C=0x9B; // Timer(s)/Counter(s) Interrupt(s) initialization TIMSK=(0<<OCIE1A) | (0<<OCIE1B) | (0<<OCIE0A) | (0<<OCIE0B) | (0<<TOIE1) | (0<<TOIE0); // External Interrupt(s) initialization // INT0: Off // Interrupt on any change on pins PCINT0-5: Off GIMSK=(0<<INT0) | (0<<PCIE); MCUCR=(0<<ISC01) | (0<<ISC00); // USI initialization // Mode: Disabled // Clock source: Register & Counter=no clk. // USI Counter Overflow Interrupt: Off USICR=(0<<USISIE) | (0<<USIOIE) | (0<<USIWM1) | (0<<USIWM0) | (0<<USICS1) | (0<<USICS0) | (0<<USICLK) | (0<<USITC); // Analog Comparator initialization // Analog Comparator: Off // The Analog Comparator's positive input is // connected to the AIN0 pin // The Analog Comparator's negative input is // connected to the AIN1 pin ACSR=(1<<ACD) | (0<<ACBG) | (0<<ACO) | (0<<ACI) | (0<<ACIE) | (0<<ACIS1) | (0<<ACIS0); ADCSRB=(0<<ACME); // Digital input buffer on AIN0: On // Digital input buffer on AIN1: On DIDR0=(0<<AIN0D) | (0<<AIN1D); // ADC initialization // ADC disabled ADCSRA=(0<<ADEN) | (0<<ADSC) | (0<<ADATE) | (0<<ADIF) | (0<<ADIE) | (0<<ADPS2) | (0<<ADPS1) | (0<<ADPS0); while (1) { // Place your code here for(i = 0; i < 155; i++) { OCR1A = i; delay_ms(30); } for(i = 154; i > 0; i--) { OCR1A = i; delay_ms(30); } } }
It looks like ATtiny10 can be configured to use ICP and OCR simultaneously in timer normal mode. You have to figure out how.there is a good chance that one timer can do both.
It depends on software and signal frequencies.
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 /******************************************************* This program was created by the CodeWizardAVR V3.28 Automatic Program Generator © Copyright 1998-2016 Pavel Haiduc, HP InfoTech s.r.l. [url]http://www.hpinfotech.com[/url] Project : pwm2pwm Version : 1.0.4 Date : 8/24/2017 Author : Company : Comments: Chip type : ATmega8 Program type : Application AVR Core Clock frequency: 8.000000 MHz Memory model : Small External RAM size : 0 Data Stack size : 256 *******************************************************/ #include <io.h> #include <stdio.h> #include <string.h> #include <delay.h> // Alphanumeric LCD functions #include <alcd.h> // Declare your global variables here #define SET 1 #define CLEAR 0 #define _LCD_COLUMN_LENGTH 16 char j = 0; unsigned int current = 0, previous = 0, period = 0, width = 0; unsigned char duty = 0; unsigned int high_time = 1000; unsigned int low_time = 250; unsigned char state_counter = 0; unsigned char do_calc = 0; char txt[23]; // Timer1 input capture interrupt service routine interrupt [TIM1_CAPT] void timer1_capt_isr(void) { // Place your code here //if(do_calc == 0) { if(state_counter == 0) { current = ICR1; TCCR1B=(0<<ICNC1) | (0<<ICES1) | (0<<WGM13) | (0<<WGM12) | (0<<CS12) | (1<<CS11) | (1<<CS10); period = current - previous; previous = current; state_counter = 1; } else if(state_counter == 1) { width = ICR1 - previous; TCCR1B=(0<<ICNC1) | (1<<ICES1) | (0<<WGM13) | (0<<WGM12) | (0<<CS12) | (1<<CS11) | (1<<CS10); state_counter = 0; //do_calc = 1; } //} } // Timer1 output compare A interrupt service routine interrupt [TIM1_COMPA] void timer1_compa_isr(void) { // Place your code here static unsigned int last_reload; //record of last value written to OCR1A static unsigned char output_mode; //record of state of OCR1A output mode, 1 = set on compare, 0 = clear on compare if (output_mode) { //OCR mode is 'set on compare' so now set delay to high time and mode to clear on compare last_reload = last_reload + high_time; OCR1AH = last_reload >> 8; OCR1AL = last_reload & 0x00ff; TCCR1A=(1<<COM1A1) | (0<<COM1A0) | (0<<COM1B1) | (0<<COM1B0) | (0<<WGM11) | (0<<WGM10); output_mode = CLEAR; } else { //OCR mode is 'clear on compare' so now set delay to low time and mode to set on compare last_reload = last_reload + low_time; OCR1AH = last_reload >> 8; OCR1AL = last_reload & 0x00ff; TCCR1A=(1<<COM1A1) | (1<<COM1A0) | (0<<COM1B1) | (0<<COM1B0) | (0<<WGM11) | (0<<WGM10); output_mode = SET; } } void main(void) { // Declare your local variables here // Input/Output Ports initialization // Port B initialization // Function: Bit7=Out Bit6=Out Bit5=Out Bit4=Out Bit3=Out Bit2=Out Bit1=Out Bit0=In DDRB=(1<<DDB7) | (1<<DDB6) | (1<<DDB5) | (1<<DDB4) | (1<<DDB3) | (1<<DDB2) | (1<<DDB1) | (0<<DDB0); // State: Bit7=0 Bit6=0 Bit5=0 Bit4=0 Bit3=0 Bit2=0 Bit1=0 Bit0=P PORTB=(0<<PORTB7) | (0<<PORTB6) | (0<<PORTB5) | (0<<PORTB4) | (0<<PORTB3) | (0<<PORTB2) | (0<<PORTB1) | (1<<PORTB0); // Port C initialization // Function: Bit6=In Bit5=Out Bit4=Out Bit3=Out Bit2=Out Bit1=Out Bit0=Out DDRC=(0<<DDC6) | (1<<DDC5) | (1<<DDC4) | (1<<DDC3) | (1<<DDC2) | (1<<DDC1) | (1<<DDC0); // State: Bit6=P Bit5=0 Bit4=0 Bit3=0 Bit2=0 Bit1=0 Bit0=0 PORTC=(1<<PORTC6) | (0<<PORTC5) | (0<<PORTC4) | (0<<PORTC3) | (0<<PORTC2) | (0<<PORTC1) | (0<<PORTC0); // Port D initialization // Function: Bit7=Out Bit6=Out Bit5=Out Bit4=Out Bit3=Out Bit2=Out Bit1=Out Bit0=Out DDRD=(1<<DDD7) | (1<<DDD6) | (1<<DDD5) | (1<<DDD4) | (1<<DDD3) | (1<<DDD2) | (1<<DDD1) | (1<<DDD0); // State: Bit7=0 Bit6=0 Bit5=0 Bit4=0 Bit3=0 Bit2=0 Bit1=0 Bit0=0 PORTD=(0<<PORTD7) | (0<<PORTD6) | (0<<PORTD5) | (0<<PORTD4) | (0<<PORTD3) | (0<<PORTD2) | (0<<PORTD1) | (0<<PORTD0); // Timer/Counter 0 initialization // Clock source: System Clock // Clock value: Timer 0 Stopped TCCR0=(0<<CS02) | (0<<CS01) | (0<<CS00); TCNT0=0x00; // Timer/Counter 1 initialization // Clock source: System Clock // Clock value: 125.000 kHz // Mode: Normal top=0xFFFF // OC1A output: Toggle on compare match // OC1B output: Disconnected // Noise Canceler: Off // Input Capture on Rising Edge // Timer Period: 0.52429 s // Output Pulse(s): // OC1A Period: 1.0486 s Width: 0.52429 s // Timer1 Overflow Interrupt: Off // Input Capture Interrupt: On // Compare A Match Interrupt: On // Compare B Match Interrupt: Off TCCR1A=(0<<COM1A1) | (1<<COM1A0) | (0<<COM1B1) | (0<<COM1B0) | (0<<WGM11) | (0<<WGM10); TCCR1B=(0<<ICNC1) | (1<<ICES1) | (0<<WGM13) | (0<<WGM12) | (0<<CS12) | (1<<CS11) | (1<<CS10); TCNT1H=0x00; TCNT1L=0x00; ICR1H=0x00; ICR1L=0x00; OCR1AH=0x00; OCR1AL=0x00; OCR1BH=0x00; OCR1BL=0x00; // Timer/Counter 2 initialization // Clock source: System Clock // Clock value: Timer2 Stopped // Mode: Normal top=0xFF // OC2 output: Disconnected ASSR=0<<AS2; TCCR2=(0<<PWM2) | (0<<COM21) | (0<<COM20) | (0<<CTC2) | (0<<CS22) | (0<<CS21) | (0<<CS20); TCNT2=0x00; OCR2=0x00; // Timer(s)/Counter(s) Interrupt(s) initialization TIMSK=(0<<OCIE2) | (0<<TOIE2) | (1<<TICIE1) | (1<<OCIE1A) | (0<<OCIE1B) | (0<<TOIE1) | (0<<TOIE0); // External Interrupt(s) initialization // INT0: Off // INT1: Off MCUCR=(0<<ISC11) | (0<<ISC10) | (0<<ISC01) | (0<<ISC00); // USART initialization // USART disabled UCSRB=(0<<RXCIE) | (0<<TXCIE) | (0<<UDRIE) | (0<<RXEN) | (0<<TXEN) | (0<<UCSZ2) | (0<<RXB8) | (0<<TXB8); // Analog Comparator initialization // Analog Comparator: Off // The Analog Comparator's positive input is // connected to the AIN0 pin // The Analog Comparator's negative input is // connected to the AIN1 pin ACSR=(1<<ACD) | (0<<ACBG) | (0<<ACO) | (0<<ACI) | (0<<ACIE) | (0<<ACIC) | (0<<ACIS1) | (0<<ACIS0); SFIOR=(0<<ACME); // ADC initialization // ADC disabled ADCSRA=(0<<ADEN) | (0<<ADSC) | (0<<ADFR) | (0<<ADIF) | (0<<ADIE) | (0<<ADPS2) | (0<<ADPS1) | (0<<ADPS0); // SPI initialization // SPI disabled SPCR=(0<<SPIE) | (0<<SPE) | (0<<DORD) | (0<<MSTR) | (0<<CPOL) | (0<<CPHA) | (0<<SPR1) | (0<<SPR0); // TWI initialization // TWI disabled TWCR=(0<<TWEA) | (0<<TWSTA) | (0<<TWSTO) | (0<<TWEN) | (0<<TWIE); // Alphanumeric LCD initialization // Connections are specified in the // Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu: // RS - PORTC Bit 0 // RD - PORTD Bit 0 // EN - PORTC Bit 1 // D4 - PORTC Bit 2 // D5 - PORTC Bit 3 // D6 - PORTC Bit 4 // D7 - PORTC Bit 5 // Characters/line: 16 lcd_init(16); lcd_gotoxy(1,0); lcd_puts(" PWM Duty "); // Globally enable interrupts #asm("sei") while (1) { // Place your code here j sprintf((char *)txt, "%3u", duty); strcat(txt, "%"); j = ((_LCD_COLUMN_LENGTH / 2) - (strlen(txt) / 2)); strcat(txt, " "); lcd_gotoxy(j,1); lcd_puts(txt); //if(do_calc == 1) { duty = (unsigned char)((float)(((float)width / (float)period) * 100.0)); high_time = (unsigned int)duty * 10; low_time = 1250 - high_time; do_calc = 0; //} } }
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 /******************************************************* This program was created by the CodeWizardAVR V3.28 Automatic Program Generator © Copyright 1998-2016 Pavel Haiduc, HP InfoTech s.r.l. [url]http://www.hpinfotech.com[/url] Project : PWM2PWM Version : 1.0.0 Date : 9/25/2017 Author : bailychic Company : NA Comments: Chip type : ATtiny10 AVR Core Clock frequency: 8.000000 MHz Memory model : Tiny External RAM size : 0 Data Stack size : 8 *******************************************************/ #include <io.h> // Declare your global variables here #define SET 1 #define CLEAR 0 unsigned int current = 0, previous = 0, period = 0, width = 0; unsigned char duty = 0; unsigned int high_time = 1000; unsigned int low_time = 250; unsigned char state_counter = 0; unsigned char do_calc = 0; // Timer 0 input capture interrupt service routine interrupt [TIM0_CAPT] void timer0_capt_isr(void) { // Place your code here if(do_calc == 0) { if(state_counter == 0) { current = ICR0; period = current - previous; previous = current; state_counter = 1; TCCR0B=(0<<ICNC0) | (0<<ICES0) | (0<<WGM03) | (0<<WGM02) | (0<<CS02) | (1<<CS01) | (1<<CS00); } else if(state_counter == 1) { width = ICR0 - previous; TCCR0B=(0<<ICNC0) | (1<<ICES0) | (0<<WGM03) | (0<<WGM02) | (0<<CS02) | (1<<CS01) | (1<<CS00); state_counter = 0; do_calc = 1; } } } // Timer 0 output compare A interrupt service routine interrupt [TIM0_COMPA] void timer0_compa_isr(void) { // Place your code here static unsigned int last_reload; //record of last value written to OCR1A static unsigned char output_mode; //record of state of OCR1A output mode, 1 = set on compare, 0 = clear on compare if (output_mode) { //OCR mode is 'set on compare' so now set delay to high time and mode to clear on compare last_reload = last_reload + high_time; OCR0AH = last_reload >> 8; OCR0AL = last_reload & 0x00ff; TCCR0A=(0<<COM0A1) | (0<<COM0A0) | (0<<COM0B1) | (0<<COM0B0) | (0<<WGM01) | (0<<WGM00); output_mode = CLEAR; } else { //OCR mode is 'clear on compare' so now set delay to low time and mode to set on compare last_reload = last_reload + low_time; OCR0AH = last_reload >> 8; OCR0AL = last_reload & 0x00ff; TCCR0A=(0<<COM0A1) | (1<<COM0A0) | (0<<COM0B1) | (0<<COM0B0) | (0<<WGM01) | (0<<WGM00); output_mode = SET; } } void main(void) { // Declare your local variables here // Main Clock source: Calibrated Internal 8 MHz Osc. CCP=0xd8; CLKMSR=(0<<CLKMS1) | (0<<CLKMS0); // Clock Prescaler division factor: 1 CCP=0xd8; CLKPSR=(0<<CLKPS3) | (0<<CLKPS2) | (0<<CLKPS1) | (0<<CLKPS0); // Voltage Level Monitor initialization // Trigger Level: Voltage Level Monitor Disabled // Interrupt: Off VLMCSR=(0<<VLMF) | (0<<VLMIE) | (0<<VLM2) | (0<<VLM1) | (0<<VLM0); // Input/Output Ports initialization // Port B initialization // Pull-up initialization: // Bit3=Off Bit2=Off Bit1=On Bit0=Off PUEB=(0<<PUEB3) | (0<<PUEB2) | (1<<PUEB1) | (0<<PUEB0); // Function: Bit3=Out Bit2=Out Bit1=In Bit0=Out DDRB=(1<<DDB3) | (1<<DDB2) | (0<<DDB1) | (1<<DDB0); // State: Bit3=0 Bit2=0 Bit1=P Bit0=0 PORTB=(0<<PORTB3) | (0<<PORTB2) | (1<<PORTB1) | (0<<PORTB0); // Break Before Make Mode PORTB: Off PORTCR=(0<<BBMB); // Timer/Counter 0 initialization // Clock source: System Clock // Clock value: 125.000 kHz // Mode: Normal top=0xFFFF // Input Capture on Rising Edge // Input Capture Noise Canceler: Off // OC0A output: Toggle on compare match // OC0B output: Disconnected // Timer Period: 0.52429 s // Output Pulse(s): // OC0A Period: 1.0486 s Width: 0.52429 s TCCR0A=(0<<COM0A1) | (1<<COM0A0) | (0<<COM0B1) | (0<<COM0B0) | (0<<WGM01) | (0<<WGM00); TCCR0B=(0<<ICNC0) | (1<<ICES0) | (0<<WGM03) | (0<<WGM02) | (0<<CS02) | (1<<CS01) | (1<<CS00); TCNT0=0x0000; ICR0=0x0000; OCR0A=0x0000; OCR0B=0x0000; // Timer/Counter 0 Interrupt(s) initialization TIMSK0=(1<<ICIE0) | (0<<OCIE0B) | (1<<OCIE0A) | (0<<TOIE0); // External Interrupt(s) initialization // INT0: Off // Interrupt on any change on pins PCINT0-3: Off EICRA=(0<<ISC01) | (0<<ISC00); EIMSK=(0<<INT0); PCICR=(0<<PCIE0); // Analog Comparator initialization // Analog Comparator: Off // The Analog Comparator's positive input is // connected to the AIN0 pin // The Analog Comparator's negative input is // connected to the AIN1 pin ACSR=(1<<ACD) | (0<<ACO) | (0<<ACI) | (0<<ACIE) | (0<<ACIC) | (0<<ACIS1) | (0<<ACIS0); // Digital input buffer on AIN0: On // Digital input buffer on AIN1: On DIDR0=(0<<ADC0D) | (0<<ADC1D); // ADC initialization // ADC disabled ADCSRA=(0<<ADEN) | (0<<ADSC) | (0<<ADATE) | (0<<ADIF) | (0<<ADIE) | (0<<ADPS2) | (0<<ADPS1) | (0<<ADPS0); // Globally enable interrupts #asm("sei") while (1) { // Place your code here if(do_calc == 1) { duty = (unsigned char)((float)(((float)width / (float)period) * 100.0)); high_time = (unsigned int)duty * 10; low_time = 1250 - high_time; do_calc = 0; } } }
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?