Diogo Cintra
/
APS4-DAC-R-2R-8-bits
Gerador de sinal do tipo rampa, triangular e senoidal
main.cpp@0:e900ae985f87, 2021-06-06 (annotated)
- Committer:
- diogonac
- Date:
- Sun Jun 06 15:29:51 2021 +0000
- Revision:
- 0:e900ae985f87
APS4 - DAC R-2R de 8-bit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
diogonac | 0:e900ae985f87 | 1 | #include "mbed.h" |
diogonac | 0:e900ae985f87 | 2 | |
diogonac | 0:e900ae985f87 | 3 | Serial pc (D1, D0); //Comunicação com USB |
diogonac | 0:e900ae985f87 | 4 | |
diogonac | 0:e900ae985f87 | 5 | BusOut saida(D4, D5, D6, D7, D8, D9, D10, D11); //Barramento dos bits do DAC R-@R |
diogonac | 0:e900ae985f87 | 6 | |
diogonac | 0:e900ae985f87 | 7 | InterruptIn troca_onda(PC_13); //Declara o botão do usuário para troca de sinal |
diogonac | 0:e900ae985f87 | 8 | |
diogonac | 0:e900ae985f87 | 9 | int senoide [] = {0x80,0x83,0x86,0x89,0x8c,0x8f,0x92,0x95, |
diogonac | 0:e900ae985f87 | 10 | 0x98,0x9b,0x9e,0xa2,0xa5,0xa7,0xaa,0xad, |
diogonac | 0:e900ae985f87 | 11 | 0xb0,0xb3,0xb6,0xb9,0xbc,0xbe,0xc1,0xc4, |
diogonac | 0:e900ae985f87 | 12 | 0xc6,0xc9,0xcb,0xce,0xd0,0xd3,0xd5,0xd7, |
diogonac | 0:e900ae985f87 | 13 | 0xda,0xdc,0xde,0xe0,0xe2,0xe4,0xe6,0xe8, |
diogonac | 0:e900ae985f87 | 14 | 0xea,0xeb,0xed,0xee,0xf0,0xf1,0xf3,0xf4, |
diogonac | 0:e900ae985f87 | 15 | 0xf5,0xf6,0xf8,0xf9,0xfa,0xfa,0xfb,0xfc, |
diogonac | 0:e900ae985f87 | 16 | 0xfd,0xfd,0xfe,0xfe,0xfe,0xff,0xff,0xff, |
diogonac | 0:e900ae985f87 | 17 | 0xff,0xff,0xff,0xff,0xfe,0xfe,0xfe,0xfd, |
diogonac | 0:e900ae985f87 | 18 | 0xfd,0xfc,0xfb,0xfa,0xfa,0xf9,0xf8,0xf6, |
diogonac | 0:e900ae985f87 | 19 | 0xf5,0xf4,0xf3,0xf1,0xf0,0xee,0xed,0xeb, |
diogonac | 0:e900ae985f87 | 20 | 0xea,0xe8,0xe6,0xe4,0xe2,0xe0,0xde,0xdc, |
diogonac | 0:e900ae985f87 | 21 | 0xda,0xd7,0xd5,0xd3,0xd0,0xce,0xcb,0xc9, |
diogonac | 0:e900ae985f87 | 22 | 0xc6,0xc4,0xc1,0xbe,0xbc,0xb9,0xb6,0xb3, |
diogonac | 0:e900ae985f87 | 23 | 0xb0,0xad,0xaa,0xa7,0xa5,0xa2,0x9e,0x9b, |
diogonac | 0:e900ae985f87 | 24 | 0x98,0x95,0x92,0x8f,0x8c,0x89,0x86,0x83, |
diogonac | 0:e900ae985f87 | 25 | 0x80,0x7c,0x79,0x76,0x73,0x70,0x6d,0x6a, |
diogonac | 0:e900ae985f87 | 26 | 0x67,0x64,0x61,0x5d,0x5a,0x58,0x55,0x52, |
diogonac | 0:e900ae985f87 | 27 | 0x4f,0x4c,0x49,0x46,0x43,0x41,0x3e,0x3b, |
diogonac | 0:e900ae985f87 | 28 | 0x39,0x36,0x34,0x31,0x2f,0x2c,0x2a,0x28, |
diogonac | 0:e900ae985f87 | 29 | 0x25,0x23,0x21,0x1f,0x1d,0x1b,0x19,0x17, |
diogonac | 0:e900ae985f87 | 30 | 0x15,0x14,0x12,0x11,0xf,0xe,0xc,0xb, |
diogonac | 0:e900ae985f87 | 31 | 0xa,0x9,0x7,0x6,0x5,0x5,0x4,0x3, |
diogonac | 0:e900ae985f87 | 32 | 0x2,0x2,0x1,0x1,0x1,0x0,0x0,0x0, |
diogonac | 0:e900ae985f87 | 33 | 0x0,0x0,0x0,0x0,0x1,0x1,0x1,0x2, |
diogonac | 0:e900ae985f87 | 34 | 0x2,0x3,0x4,0x5,0x5,0x6,0x7,0x9, |
diogonac | 0:e900ae985f87 | 35 | 0xa,0xb,0xc,0xe,0xf,0x11,0x12,0x14, |
diogonac | 0:e900ae985f87 | 36 | 0x15,0x17,0x19,0x1b,0x1d,0x1f,0x21,0x23, |
diogonac | 0:e900ae985f87 | 37 | 0x25,0x28,0x2a,0x2c,0x2f,0x31,0x34,0x36, |
diogonac | 0:e900ae985f87 | 38 | 0x39,0x3b,0x3e,0x41,0x43,0x46,0x49,0x4c, |
diogonac | 0:e900ae985f87 | 39 | 0x4f,0x52,0x55,0x58,0x5a,0x5d,0x61,0x64, |
diogonac | 0:e900ae985f87 | 40 | 0x67,0x6a,0x6d,0x70,0x73,0x76,0x79,0x7c}; //Look Up table para gerar um sinal senoidal |
diogonac | 0:e900ae985f87 | 41 | |
diogonac | 0:e900ae985f87 | 42 | int i, cont; |
diogonac | 0:e900ae985f87 | 43 | |
diogonac | 0:e900ae985f87 | 44 | void rotina_troca_onda(void); //Rotina para trocar o sinal exibido |
diogonac | 0:e900ae985f87 | 45 | |
diogonac | 0:e900ae985f87 | 46 | void rotina_rampa(void); //Rotina para gerar um sinal de uma rampa |
diogonac | 0:e900ae985f87 | 47 | void rotina_triangular(void); //Rotina para gerar um sinal triangular |
diogonac | 0:e900ae985f87 | 48 | void rotina_senoidal(void); //Rotina para gerar um sinal de uma senoide |
diogonac | 0:e900ae985f87 | 49 | |
diogonac | 0:e900ae985f87 | 50 | int main() |
diogonac | 0:e900ae985f87 | 51 | { |
diogonac | 0:e900ae985f87 | 52 | pc.baud(115200); //Define a velocidade da porta USB |
diogonac | 0:e900ae985f87 | 53 | |
diogonac | 0:e900ae985f87 | 54 | troca_onda.fall(&rotina_troca_onda); //Define quando o botão vai ser detectado |
diogonac | 0:e900ae985f87 | 55 | |
diogonac | 0:e900ae985f87 | 56 | while(1) { |
diogonac | 0:e900ae985f87 | 57 | |
diogonac | 0:e900ae985f87 | 58 | if(cont == 0)rotina_rampa(); |
diogonac | 0:e900ae985f87 | 59 | if(cont == 1)rotina_triangular(); |
diogonac | 0:e900ae985f87 | 60 | if(cont == 2)rotina_senoidal(); |
diogonac | 0:e900ae985f87 | 61 | if(cont >= 3) cont = 0; |
diogonac | 0:e900ae985f87 | 62 | |
diogonac | 0:e900ae985f87 | 63 | } |
diogonac | 0:e900ae985f87 | 64 | } |
diogonac | 0:e900ae985f87 | 65 | |
diogonac | 0:e900ae985f87 | 66 | void rotina_troca_onda() |
diogonac | 0:e900ae985f87 | 67 | { |
diogonac | 0:e900ae985f87 | 68 | cont ++; |
diogonac | 0:e900ae985f87 | 69 | } |
diogonac | 0:e900ae985f87 | 70 | |
diogonac | 0:e900ae985f87 | 71 | void rotina_rampa() |
diogonac | 0:e900ae985f87 | 72 | { |
diogonac | 0:e900ae985f87 | 73 | for(i = 0; i <= 255; i++) { |
diogonac | 0:e900ae985f87 | 74 | saida = i; |
diogonac | 0:e900ae985f87 | 75 | wait(0.001); |
diogonac | 0:e900ae985f87 | 76 | } |
diogonac | 0:e900ae985f87 | 77 | } |
diogonac | 0:e900ae985f87 | 78 | |
diogonac | 0:e900ae985f87 | 79 | void rotina_triangular() |
diogonac | 0:e900ae985f87 | 80 | { |
diogonac | 0:e900ae985f87 | 81 | for(i = 0; i <= 255; i++) { |
diogonac | 0:e900ae985f87 | 82 | saida = i; |
diogonac | 0:e900ae985f87 | 83 | wait(0.001); |
diogonac | 0:e900ae985f87 | 84 | } |
diogonac | 0:e900ae985f87 | 85 | |
diogonac | 0:e900ae985f87 | 86 | for(i = 255; i > -1; i--) { |
diogonac | 0:e900ae985f87 | 87 | saida = i; |
diogonac | 0:e900ae985f87 | 88 | wait(0.001); |
diogonac | 0:e900ae985f87 | 89 | } |
diogonac | 0:e900ae985f87 | 90 | } |
diogonac | 0:e900ae985f87 | 91 | |
diogonac | 0:e900ae985f87 | 92 | void rotina_senoidal() |
diogonac | 0:e900ae985f87 | 93 | { |
diogonac | 0:e900ae985f87 | 94 | for(i = 0; i <= 255; i++) { |
diogonac | 0:e900ae985f87 | 95 | saida = senoide[i]; |
diogonac | 0:e900ae985f87 | 96 | wait(0.001); |
diogonac | 0:e900ae985f87 | 97 | } |
diogonac | 0:e900ae985f87 | 98 | } |