Revision: 1266 Author: robhamerling Date: Sat Aug 29 05:44:33 2009 Log: Samples demonstrating the new PWM libraries with two ADC channesl and two PWM channels with high resolution or high frequency.
http://code.google.com/p/jallib/source/detail?r=1266 Added: /trunk/sample/16f886_pwm_adc_freq.jal /trunk/sample/16f886_pwm_adc_res.jal ======================================= --- /dev/null +++ /trunk/sample/16f886_pwm_adc_freq.jal Sat Aug 29 05:44:33 2009 @@ -0,0 +1,130 @@ +-- ------------------------------------------------------ +-- Title: Test program for testing a combination of PWM and ADC (2 channels both) +-- +-- Author: Rob Hamerling, Copyright (c) 2009..2009, all rights reserved. +-- +-- Adapted-by: +-- +-- Compiler: 2.4l +-- +-- This file is part of jallib (http://jallib.googlecode.com) +-- Released under the BSD license (http://www.opensource.org/licenses/bsd-license.php) +-- +-- Description: +-- Using a combination of 2 ADC and PWM channels. +-- First ADC-PWM combination is used for LCD backlight, +-- second combination uses an LED as output. +-- Two different PWM frequencies are set, both allowing only low +-- resolution PWM dutycycle setting. +-- The channels are (should be!) completely independent. +-- An LCD display is used to show some debugging data. +-- +-- Sources: +-- +-- Notes: +-- 1. HD44780 compatible display, used in 4-bit mode. +-- The LCD backlight is controlled directly (not with the backlight library) +-- +-- ------------------------------------------------------ + +include 16f886 + +enable_digital_io() -- disable analog I/O (if any) + +-- +-- This program assumes a 20 MHz resonator or crystal +-- is connected to pins OSC1 and OSC2. +pragma target OSC HS -- HS crystal or resonator +pragma target clock 20_000_000 -- oscillator frequency +pragma target MCLR external +pragma target WDT disabled +pragma target LVP disabled + + +-- -------- LCD setup ----------- +const byte LCD_ROWS = 2 -- 2 lines +const byte LCD_CHARS = 16 -- 16 chars per line + +alias lcd_D4 is pin_B0 -- LCD data port +alias lcd_D5 is pin_B1 -- LCD data port +alias lcd_D6 is pin_B2 -- LCD data port +alias lcd_D7 is pin_B3 -- LCD data port +alias lcd_EN is pin_C5 -- LCD command/data select. +alias lcd_RS is pin_C4 -- LCD data trigger +PORTB_low_direction = all_output +PORTC_high_direction = all_output + +include lcd_hd44780_4 -- include LCD library +lcd_init() -- initialize LCD + +-- -------- ADC setup ----------- +const ADC_CHANNEL_A = 11 -- AN11 (pin_B4) +const ADC_CHANNEL_B = 13 -- AN13 (pin_B5) +const byte ADC_NVREF = 0 -- no external Vref +const word ADC_RSOURCE = 5_000 -- 5K potmeter +const bit ADC_HIGH_RESOLUTION = TRUE -- high resolution +include adc -- include ADC library +adc_init() -- init library +set_analog_pin(ADC_CHANNEL_A) -- init used ADC channel +set_analog_pin(ADC_CHANNEL_B) -- init used ADC channel + +-- ---- PWM setup ------- +pin_CCP1_direction = output -- set PWM-pins output! +pin_CCP2_direction = output +include pwm_hardware + +include print -- data formatting +include format -- data formatting +include delay -- delay library + +const byte strPR2[] = "PR2=" +const byte strR1L[] = "R1L=" +const byte strR2L[] = "R2L=" + +var word measure_A -- ADC channel A +var word measure_B -- ADC channel B + +procedure show_data(byte in no) is + lcd_clear_screen() + lcd = "0" + no + lcd = "." + print_string(lcd, strPR2) + format_byte_hex(lcd, PR2) + format_word_dec(lcd, measure_A, 4, 0) + format_word_dec(lcd, measure_B, 4, 0) + lcd_cursor_position(1,0) + print_string(lcd, strR1L) + format_byte_hex(lcd, CCPR1L) + lcd = " " + print_string(lcd, strR2L) + format_byte_hex(lcd, CCPR2L) + delay_100ms(30) +end procedure + +forever loop + + measure_A = adc_read(ADC_CHANNEL_A) -- read potmeter 1 + measure_B = adc_read(ADC_CHANNEL_B) -- read potmeter 2 + + pwm_set_frequency(75000) -- set high PWM frequency + + pwm1_set_dutycycle_lowres(byte(measure_A / 4)) -- max 8 bits! + pwm2_set_dutycycle_lowres(byte(measure_B / 4)) + show_data(1) + + pwm1_set_percent_dutycycle(byte(measure_A / 32)) -- limit value! + pwm2_set_percent_dutycycle(byte(measure_B / 32)) + show_data(2) + + pwm_set_frequency(150000) -- set high PWM frequency + + pwm1_set_dutycycle_lowres(byte(measure_A / 8)) -- max 7 bits! + pwm2_set_dutycycle_lowres(byte(measure_B / 8)) + show_data(3) + + pwm1_set_percent_dutycycle(byte(measure_A / 32)) -- limit value! + pwm2_set_percent_dutycycle(byte(measure_B / 32)) + show_data(4) + +end loop + ======================================= --- /dev/null +++ /trunk/sample/16f886_pwm_adc_res.jal Sat Aug 29 05:44:33 2009 @@ -0,0 +1,128 @@ +-- ------------------------------------------------------ +-- Title: Test program for testing a combination of PWM and ADC (2 channels both) +-- +-- Author: Rob Hamerling, Copyright (c) 2009..2009, all rights reserved. +-- +-- Adapted-by: +-- +-- Compiler: 2.4l +-- +-- This file is part of jallib (http://jallib.googlecode.com) +-- Released under the BSD license (http://www.opensource.org/licenses/bsd-license.php) +-- +-- Description: +-- Using a combination of 2 ADC and PWM channels. +-- First ADC-PWM combination is used for LCD backlight, +-- second combination uses an LED as output. +-- The PWM output uses maximum (10 bits) resolution, +-- at the highest possible frequency. +-- The channels are (should be!) completely independent. +-- An LCD display is used to show some debugging data. +-- +-- Sources: +-- +-- Notes: +-- 1. HD44780 compatible display, used in 4-bit mode. +-- The LCD backlight is controlled directly (not with the backlight library) +-- +-- ------------------------------------------------------ + +include 16f886 + +enable_digital_io() -- disable analog I/O (if any) + +-- +-- This program assumes a 20 MHz resonator or crystal +-- is connected to pins OSC1 and OSC2. +pragma target OSC HS -- HS crystal or resonator +pragma target clock 20_000_000 -- oscillator frequency +pragma target MCLR external +pragma target WDT disabled +pragma target LVP disabled + + +-- -------- LCD setup ----------- +const byte LCD_ROWS = 2 -- 2 lines +const byte LCD_CHARS = 16 -- 16 chars per line + +alias lcd_D4 is pin_B0 -- LCD data port +alias lcd_D5 is pin_B1 -- LCD data port +alias lcd_D6 is pin_B2 -- LCD data port +alias lcd_D7 is pin_B3 -- LCD data port +alias lcd_EN is pin_C5 -- LCD command/data select. +alias lcd_RS is pin_C4 -- LCD data trigger +PORTB_low_direction = all_output +PORTC_high_direction = all_output + +include lcd_hd44780_4 -- include LCD library +lcd_init() -- initialize LCD + +-- -------- ADC setup ----------- +const ADC_CHANNEL_A = 11 -- AN11 (pin_B4) +const ADC_CHANNEL_B = 13 -- AN13 (pin_B5) +const byte ADC_NVREF = 0 -- no external Vref +const word ADC_RSOURCE = 5_000 -- 5K potmeter +const bit ADC_HIGH_RESOLUTION = TRUE -- high resolution +include adc -- include ADC library +adc_init() -- init library +set_analog_pin(ADC_CHANNEL_A) -- init used ADC channel +set_analog_pin(ADC_CHANNEL_B) -- init used ADC channel + +-- ---- PWM setup ------- +pin_CCP1_direction = output -- PWM-pin is output! +pin_CCP2_direction = output -- PWM-pin is output! +include pwm_hardware + +include print -- data formatting +include format -- data formatting +include delay -- delay library + +const byte strPR2[] = "PR2=" +const byte strR1L[] = "R1L=" +const byte strR2L[] = "R2L=" + +var word measure_A -- ADC measurement value +var word measure_B -- ADC measurement value + +procedure show_data(byte in no) is + lcd_clear_screen() + lcd = "0" + no + lcd = "." + print_string(lcd, strPR2) + format_byte_hex(lcd, PR2) + format_word_dec(lcd, measure_A, 4, 0) + format_word_dec(lcd, measure_B, 4, 0) + lcd_cursor_position(1,0) + print_string(lcd, strR1L) + format_byte_hex(lcd, CCPR1L) + lcd = " " + print_string(lcd, strR2L) + format_byte_hex(lcd, CCPR2L) + delay_100ms(30) +end procedure + +pwm_max_resolution(1) -- set 10-bits resolution + +forever loop + + measure_A = adc_read(ADC_CHANNEL_A) -- read potmeter 1 + measure_B = adc_read(ADC_CHANNEL_B) -- read potmeter 2 + + pwm1_set_dutycycle_highres(measure_A) + pwm2_set_dutycycle_highres(measure_B) + show_data(1) + + pwm1_set_dutycycle(byte(measure_A / 4)) + pwm2_set_dutycycle(byte(measure_B / 4)) + show_data(2) + + pwm1_set_dutycycle_lowres(byte(measure_A * 4)) + pwm2_set_dutycycle_lowres(byte(measure_B * 4)) + show_data(3) + + pwm1_set_percent_dutycycle(byte(measure_A * 25 / 256)) -- (factor 100/1024) + pwm2_set_percent_dutycycle(byte(measure_B * 25 / 256)) + show_data(4) + +end loop + --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "jallib" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/jallib?hl=en -~----------~----~----~----~------~----~------~--~---
