Rambam sets out the institution of fast days, when the community fasts, and the public prayer and repentance associated with them. This provides an early halakhic framework for davening on a fast day.
וּמִדִּבְרֵי סוֹפְרִים לְהִתְעַנּוֹת עַל כָּל צָרָה שֶׁתָּבוֹא עַל הַצִּבּוּר עַד שֶׁיְּרֻחֲמוּ מִן הַשָּׁמַיִם. וּבִימֵי הַתַּעֲנִיּוֹת הָאֵלּוּ זוֹעֲקִין בִּתְפִלּוֹת וּמִתְחַנְּנִים וּמְרִיעִין בַּחֲצוֹצְרוֹת בִּלְבַד. וְאִם הָיוּ בַּמִּקְדָּשׁ מְרִיעִין בַּחֲצוֹצְרוֹת וּבְשׁוֹפָר. הַשּׁוֹפָר מְקַצֵּר וְהַחֲצוֹצְרוֹת מַאֲרִיכוֹת. שֶׁמִּצְוַת הַיּוֹם בַּחֲצוֹצְרוֹת. וְאֵין תּוֹקְעִין בַּחֲצוֹצְרוֹת וְשׁוֹפָר כְּאֶחָד אֶלָּא בַּמִּקְדָּשׁ שֶׁנֶּאֱמַר (תהילים צח ו) "בַּחֲצֹצְרוֹת וְקוֹל שׁוֹפָר הָרִיעוּ לִפְנֵי הַמֶּלֶךְ ה'": כְּשֵׁם שֶׁהַצִּבּוּר מִתְעַנִּים עַל צָרָתָן כָּךְ הַיָּחִיד מִתְעַנֶּה עַל צָרָתוֹ. כֵּיצַד. הֲרֵי שֶׁהָיָה לוֹ חוֹלֶה אוֹ תּוֹעֶה בַּמִּדְבָּר אוֹ אָסוּר בְּבֵית הָאֲסוּרִין. יֵשׁ לוֹ לְהִתְעַנּוֹת עָלָיו וּלְבַקֵּשׁ רַחֲמִים בִּתְפִלָּתוֹ. וְאוֹמֵר עֲנֵנוּ וְכוּ' בְּכָל תְּפִלָּה שֶׁמִּתְפַּלֵּל. וְלֹא יִתְעַנֶּה בְּשַׁבָּתוֹת וְלֹא בְּמוֹעֲדוֹת וְלֹא בְּרָאשֵׁי חֳדָשִׁים וְלֹא בַּחֲנֻכָּה וּפוּרִים:
In addition, it is a Rabbinic ordinance to fast whenever there is a difficulty that affects the community until there is a manifestation of Divine mercy. On these fast days, we cry out in prayer, offer supplications, and sound the trumpets only. In the Temple, we sound both the trumpets and the shofar. The shofar blasts should be shortened and the trumpet blasts extended, for the mitzvah of the day is with the trumpets. The trumpets are sounded together with the shofar only in the Temple, as [can be inferred from Psalms 98:6]: "Sound trumpets and shofar blasts before God, the King." Just as the community should fast because of distress, so too, each individual should fast [when confronted by] distress. What is implied? When an individual to whom a person [feels close] is sick, lost in the desert, or imprisoned, one should fast for his sake, ask for mercy for him in prayer, and say [the passage] Anenu in all the Shemoneh Esreh prayers recited [that day]. One should not fast on the Sabbath, on festivals, on Rosh Chodesh, on Chanukah, or on Purim.